All JavaScript Strings Methods ( Cheatsheet )

Mayur PatilMayur Patil
2 min read
  1. length: Returns the length of a string. Example: "Hello".length returns 5.

  2. charAt(index): Returns the character at the specified index in a string. Example: "Hello".charAt(1) returns "e".

  3. concat(str1, str2, ...): Combines two or more strings and returns a new string. Example: "Hello".concat(" ", "world!") returns "Hello world!".

  4. toUpperCase(): Converts a string to uppercase. Example: "hello".toUpperCase() returns "HELLO".

  5. toLowerCase(): Converts a string to lowercase. Example: "HELLO".toLowerCase() returns "hello".

  6. trim(): Removes whitespace from both ends of a string. Example: " Hello ".trim() returns "Hello".

  7. split(separator, limit): Splits a string into an array of substrings based on a specified separator. Example: "Hello,world".split(",") returns ["Hello", "world"].

  8. indexOf(searchValue, startIndex): Returns the index of the first occurrence of a specified value in a string. Example: "Hello".indexOf("o") returns 4.

  9. replace(searchValue, newValue): Replaces a specified value with another value in a string. Example: "Hello".replace("o", "i") returns "Helli".

  10. substring(startIndex, endIndex): Returns a substring based on the specified start and end index. Example: "Hello".substring(1, 4) returns "ell".

  11. slice(startIndex, endIndex): Returns a portion of a string starting from the specified start index to the specified end index. Example: "Hello".slice(1, 4) returns "ell".

  12. startsWith(searchString): Checks if a string starts with the specified search string and returns a boolean value. Example: "Hello".startsWith("H") returns true.

  13. endsWith(searchString): Checks if a string ends with the specified search string and returns a boolean value. Example: "Hello".endsWith("o") returns true.

  14. includes(searchString): Checks if a string contains the specified search string and returns a boolean value. Example: "Hello".includes("ell") returns true.

1
Subscribe to my newsletter

Read articles from Mayur Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mayur Patil
Mayur Patil

Frontend Developer || UI Developer || JavaScript || React