JavaScript String Methods

Subject: JavaScript

JavaScript provides a rich set of string methods to manipulate and process text effectively. These methods help perform operations such as searching, replacing, extracting, and formatting strings.


1. length

Returns the number of characters in the string.

2. charAt(index)

Returns the character at the specified index.

3. charCodeAt(index)

Returns the Unicode value of the character at the specified index.

4. slice(start, end)

Extracts a section of the string and returns it as a new string.

5. substring(start, end)

Similar to slice(), but does not accept negative indexes.

6. substr(start, length)

Extracts part of a string starting at the specified index for a given length.

Note: substr() is considered legacy; prefer slice() or substring().

7. toUpperCase()

Converts the string to uppercase.

8. toLowerCase()

Converts the string to lowercase.

9. trim()

Removes whitespace from both ends of the string.

10. replace(search, replacement)

Replaces the first match of a substring.

11. replaceAll(search, replacement)

Replaces all matches of a substring.

12. concat()

Combines two or more strings.

13. includes(searchString)

Checks if a substring exists within the string.

14. startsWith(substring)

Checks if a string begins with the specified substring.

15. endsWith(substring)

Checks if a string ends with the specified substring.

16. split(separator)

Splits a string into an array of substrings.

17. repeat(count)

Repeats a string a specified number of times.

18. match(regex)

Returns an array of matches against a regular expression.

19. indexOf() and lastIndexOf()


Key Takeaways

  • JavaScript offers numerous string methods for text manipulation, search, and transformation.
  • Use slice(), substring(), or substr() to extract parts of a string.
  • Use includes(), startsWith(), and endsWith() for substring checks.
  • Use replace() and replaceAll() to modify string content.
  • Strings are immutable; string methods return new strings without modifying the original.
Next : JS Number