DEV Community

Shreya Shivratriwar
Shreya Shivratriwar

Posted on

Understanding JavaScript String Methods: A Comprehensive Guide

JavaScript, the language of the web, offers a wide array of powerful methods to manipulate strings, making it an indispensable tool for developers. In this blog post, we will delve into the corrected version of some essential JavaScript string methods, exploring how they work and how they can be applied to real-world scenarios.

  1. charAt()
    charAt() returns the character at a specific index within a string. In the given example, stringOne.charAt(1) returns "r".

  2. charCodeAt()
    charCodeAt() returns the Unicode value of the character at a specified index. For instance, stringOne.charCodeAt(1) returns 114, which corresponds to the Unicode value of "r".

  3. concat()
    concat() concatenates two or more strings and returns a new string. In this case, stringOne.concat(stringTwo) results in "freeCodeCamp is the best place to learnfrontend and backend development".

  4. endsWith()
    endsWith() checks if a string ends with a specific substring and returns true or false. Here, stringOne.endsWith("learn") evaluates to true.

  5. fromCharCode()
    String.fromCharCode() converts Unicode values to characters. For example, String.fromCharCode(114) returns "r".

  6. includes()
    includes() determines whether a string contains a specific substring, returning true or false. In the given code, stringTwo.includes("end") results in true.

  7. indexOf()
    indexOf() finds the first occurrence of a specified substring in a string and returns its index. Here, stringTwo.indexOf("end") returns 5.

  8. lastIndexOf()
    lastIndexOf() finds the last occurrence of a specified substring in a string and returns its index. In this case, stringTwo.lastIndexOf("end") yields 17.

  9. match()
    match() searches a string for a specified pattern using a regular expression and returns an array of matches. For instance, stringTwo.match(/end/g) results in ["end", "end"].

  10. repeat()
    repeat() creates a new string by repeating the original string a specified number of times. Here, stringTwo.repeat(3) repeats the string three times.

  11. replace()
    replace() replaces specified substrings within a string. In this example, stringTwo.replace(/end/g, "END") changes all occurrences of "end" to "END".

  12. search()
    search() searches for a specified value in a string and returns its index. Here, stringTwo.search("end") returns 5.

  13. slice()
    slice() extracts a section of a string and returns it as a new string. stringTwo.slice(2, 4) gives "on".

  14. split()
    split() divides a string into an array of substrings based on a specified delimiter. stringOne.split(" ") results in ["freeCodeCamp", "is", "the", "best", "place", "to", "learn"].

  15. startsWith()
    startsWith() checks if a string starts with a specific substring and returns true or false. Here, stringOne.startsWith("free") evaluates to true.

  16. substr()
    substr() extracts a specified portion of a string. stringTwo.substr(2, 4) gives "onte".

  17. substring()
    substring() retrieves a subset of a string between two specified indices. In this case, stringTwo.substring(2, 4) returns "on".

  18. toUpperCase()
    toUpperCase() converts a string to uppercase. stringOne.toUpperCase() results in "FREECODECAMP IS THE BEST PLACE TO LEARN".

  19. toLowerCase()
    toLowerCase() converts a string to lowercase. stringOne.toLowerCase() gives "freecodecamp is the best place to learn".

  20. trim()
    trim() removes whitespace from both ends of a string. stringThree.trim() results in "subscribe now!".

Understanding these methods and their applications is essential for every JavaScript developer. With this knowledge, you can enhance your coding skills and create more efficient, dynamic, and user-friendly web applications. Happy coding!

Top comments (0)