JavaScript String Methods Explained


JavaScript string methods are built-in functions that allow you to manipulate and work with strings in various ways. 

These methods make it easier to perform operations like finding, replacing, splitting, or modifying strings.

Common JavaScript String Methods

  1. length

    • Returns the number of characters in a string.

    let text = "JavaScript"; console.log(text.length); // Outputs: 10
  2. toUpperCase()

    • Converts all characters in a string to uppercase.

    let text = "JavaScript"; console.log(text.toUpperCase()); // Outputs: "JAVASCRIPT"
  3. toLowerCase()

    • Converts all characters in a string to lowercase.

    let text = "JavaScript"; console.log(text.toLowerCase()); // Outputs: "javascript"
  4. charAt(index)

    • Returns the character at a specific index in a string.

    let text = "JavaScript"; console.log(text.charAt(0)); // Outputs: "J"
  5. indexOf(substring)

    • Returns the index of the first occurrence of a specified substring, or -1 if the substring is not found.

    let text = "JavaScript"; console.log(text.indexOf("Script")); // Outputs: 4
  6. lastIndexOf(substring)

    • Returns the index of the last occurrence of a specified substring, or -1 if not found.

    let text = "JavaScript is great, and I love JavaScript!"; console.log(text.lastIndexOf("JavaScript")); // Outputs: 34
  7. includes(substring)

    • Returns true if a string contains a specified substring, otherwise false.

    let text = "JavaScript is fun"; console.log(text.includes("fun")); // Outputs: true
  8. startsWith(substring)

    • Returns true if a string starts with a specified substring, otherwise false.

    let text = "JavaScript"; console.log(text.startsWith("Java")); // Outputs: true
  9. endsWith(substring)

    • Returns true if a string ends with a specified substring, otherwise false.

    let text = "JavaScript"; console.log(text.endsWith("Script")); // Outputs: true
  10. substring(start, end)

    • Extracts characters from a string between two specified indices.

    let text = "JavaScript"; let part = text.substring(0, 4); // "Java"
  11. slice(start, end)

    • Similar to substring, but can also accept negative indices to count from the end.

    let text = "JavaScript"; let part = text.slice(4); // "Script"
  12. replace(searchValue, newValue)

    • Replaces the first occurrence of a specified substring with a new value.

    let text = "JavaScript is great"; let newText = text.replace("great", "awesome"); console.log(newText); // Outputs: "JavaScript is awesome"
  13. replaceAll(searchValue, newValue)

    • Replaces all occurrences of a specified substring with a new value.

    let text = "JavaScript is great. JavaScript is popular."; let newText = text.replaceAll("JavaScript", "JS"); console.log(newText); // Outputs: "JS is great. JS is popular."
  14. split(separator)

    • Splits a string into an array of substrings based on a specified separator.

    let text = "JavaScript,HTML,CSS"; let parts = text.split(","); console.log(parts); // Outputs: ["JavaScript", "HTML", "CSS"]
  15. trim()

    • Removes whitespace from both ends of a string.

    let text = " JavaScript "; console.log(text.trim()); // Outputs: "JavaScript"
  16. trimStart() (or trimLeft())

    • Removes whitespace from the beginning of a string.

    let text = " JavaScript"; console.log(text.trimStart()); // Outputs: "JavaScript"
  17. trimEnd() (or trimRight())

    • Removes whitespace from the end of a string.

    let text = "JavaScript "; console.log(text.trimEnd()); // Outputs: "JavaScript"
  18. concat(string1, string2, ...)

    • Joins two or more strings and returns a new concatenated string.

    let text1 = "Hello"; let text2 = "World"; let result = text1.concat(", ", text2); console.log(result); // Outputs: "Hello, World"
  19. repeat(count)

    • Returns a new string with a specified number of copies of the original string.

    let text = "JavaScript"; console.log(text.repeat(3)); // Outputs: "JavaScriptJavaScriptJavaScript"
  20. padStart(targetLength, padString)

    • Pads the current string with another string until it reaches the target length, starting from the beginning.

    let text = "5"; console.log(text.padStart(3, "0")); // Outputs: "005"
  21. padEnd(targetLength, padString)

    • Pads the current string with another string until it reaches the target length, starting from the end.

    let text = "5"; console.log(text.padEnd(3, "0")); // Outputs: "500"
  22. match(regex)

    • Searches a string for a match against a regular expression and returns the matches.

    let text = "The rain in Spain"; let result = text.match(/ain/g); console.log(result); // Outputs: ["ain", "ain"]
  23. search(regex)

    • Searches a string for a match against a regular expression and returns the index of the match, or -1 if not found.

    let text = "JavaScript"; console.log(text.search(/Script/)); // Outputs: 4
  24. toString()

    • Returns the string representation of a value.

    let number = 123; console.log(number.toString()); // Outputs: "123"
  25. valueOf()

    • Returns the primitive value of a string object.

    let text = new String("Hello"); console.log(text.valueOf()); // Outputs: "Hello"

These examples cover a wide range of string manipulation techniques available in JavaScript, providing powerful tools for working with text in your programs.

Previous Post Next Post

Welcome, New Friend!

We're excited to have you here for the first time!

Enjoy your colorful journey with us!

Welcome Back!

Great to see you Again

If you like the content share to help someone

Thanks

Contact Form