WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

JavaScript Β· Basic Β· question 19 of 100

What are some common string methods in JavaScript?

πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

In JavaScript, strings are a primitive data type and come with a variety of built-in methods that allow you to manipulate and work with strings. Here are some of the most common string methods in JavaScript:

length: This method returns the length of a string.

    var myString = "Hello, World!";
    console.log(myString.length); // Outputs 13

charAt(): This method returns the character at a specified index in a string.

    var myString = "Hello, World!";
    console.log(myString.charAt(1)); // Outputs "e"

substring(): This method returns a substring of a string, starting at a specified index and ending at a specified index.

    var myString = "Hello, World!";
    console.log(myString.substring(0, 5)); // Outputs "Hello"

indexOf(): This method returns the index of the first occurrence of a specified substring in a string.

    var myString = "Hello, World!";
    console.log(myString.indexOf("World")); // Outputs 7

toUpperCase() and toLowerCase(): These methods convert a string to all uppercase or lowercase letters.

    var myString = "Hello, World!";
    console.log(myString.toUpperCase()); // Outputs "HELLO, WORLD!"
    console.log(myString.toLowerCase()); // Outputs "hello, world!"

split(): This method splits a string into an array of substrings based on a specified separator.

    var myString = "Hello, World!";
    console.log(myString.split(" ")); // Outputs ["Hello,", "World!"]

replace(): This method replaces a specified substring with another substring in a string.

    var myString = "Hello, World!";
    console.log(myString.replace("World", "Universe")); // Outputs "Hello, Universe!"

In addition to these methods, there are many other string methods available in JavaScript. Understanding how to use these methods can be very helpful in manipulating and working with strings in your JavaScript code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview β€” then scores it.
πŸ“ž Practice JavaScript β€” free 15 min
πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

All 100 JavaScript questions Β· All topics