[JavaScript] How do I manipulate strings in JavaScript (e.g., concatenation, length)?

To manipulate strings in JavaScript, you can use various built-in methods and operators. Here are a few examples:

  1. Concatenation (+ operator): You can use the + operator to concatenate two or more strings together:

    1const str1 = "Hello";
    2const str2 = "World";
    3const result = str1 + " " + str2;
    4console.log(result); // Output: "Hello World"
  2. String length (length property): You can use the length property to get the length of a string:

    1const str = "Hello World";
    2console.log(str.length); // Output: 11
  3. Accessing individual characters (charAt() method): You can use the charAt() method to access a character at a specific index of a string:

    1const str = "Hello";
    2console.log(str.charAt(0)); // Output: "H"
    3console.log(str.charAt(3)); // Output: "l"
  4. Substring (substring() method): You can use the substring() method to extract a portion of a string based on starting and ending indexes:

    1const str = "Hello World";
    2console.log(str.substring(6)); // Output: "World"
    3console.log(str.substring(0, 5)); // Output: "Hello"
  5. Replacing substrings (replace() method): You can use the replace() method to replace a specific substring with another substring:

    1const str = "Hello World";
    2const newStr = str.replace("World", "JavaScript");
    3console.log(newStr); // Output: "Hello JavaScript"

These are just a few examples of string manipulation in JavaScript. There are many more methods available based on specific requirements.