DEV Community

A Ramesh
A Ramesh

Posted on

Complete List of JavaScript String Methods

All string methods return a new string or value β€” they do not modify the original string.

πŸ”€ Basic Info

  • length β†’ Returns the length of a string.
"hello".length;  // 5
Enter fullscreen mode Exit fullscreen mode

🧾 Case Conversion

  • toUpperCase() β†’ Converts to uppercase.
  • toLowerCase() β†’ Converts to lowercase.
"hello".toUpperCase();  // "HELLO"
"HELLO".toLowerCase();  // "hello"
Enter fullscreen mode Exit fullscreen mode

βœ‚οΈ Extracting Parts of a String

  • slice(start, end) β†’ Extracts part of the string.
  • substring(start, end) β†’ Similar to slice but no negative index.
  • substr(start, length) (deprecated) β†’ Extracts a substring.
"JavaScript".slice(0, 4);     // "Java"
"JavaScript".substring(4, 10); // "Script"
"JavaScript".substr(4, 6);    // "Script"
Enter fullscreen mode Exit fullscreen mode

πŸ” Searching & Matching

  • indexOf(substring) β†’ First index of a match.
  • lastIndexOf(substring) β†’ Last index of a match.
  • includes(substring) β†’ Returns true if found.
  • startsWith(substring) β†’ Checks beginning.
  • endsWith(substring) β†’ Checks ending.
  • search(regexp) β†’ Searches for match with regex.
  • match(regexp) β†’ Returns matching string(s) with regex.
  • matchAll(regexp) β†’ Returns all matches (ES2020+).
"hello world".includes("world");      // true
"hello world".startsWith("hello");    // true
"hello world".endsWith("world");      // true
"hello world".indexOf("o");           // 4
Enter fullscreen mode Exit fullscreen mode

🧼 Trimming Whitespace

  • trim() β†’ Removes whitespace from both ends.
  • trimStart() / trimLeft() β†’ From start.
  • trimEnd() / trimRight() β†’ From end.
"  hello  ".trim();       // "hello"
"  hello  ".trimStart();  // "hello  "
"  hello  ".trimEnd();    // "  hello"
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Replacing

  • replace(search, replace) β†’ Replace first match.
  • replaceAll(search, replace) β†’ Replace all matches (ES2021+).
"apple banana".replace("banana", "orange");     // "apple orange"
"a-b-c".replaceAll("-", ",");                   // "a,b,c"
Enter fullscreen mode Exit fullscreen mode

πŸ”— Joining / Splitting

  • concat(string2, string3...) β†’ Join strings.
  • split(separator) β†’ Split into array.
"Hello".concat(" ", "World"); // "Hello World"
"one,two,three".split(",");   // ["one", "two", "three"]
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Character Access

  • charAt(index) β†’ Returns character at index.
  • charCodeAt(index) β†’ Returns Unicode of character.
  • codePointAt(index) β†’ Returns full Unicode code point.
"Java".charAt(0);       // "J"
"Java".charCodeAt(0);   // 74
Enter fullscreen mode Exit fullscreen mode

πŸ”  String Creation & Repetition

  • repeat(n) β†’ Repeats string n times.
  • fromCharCode(code1, code2, ...) (Static) β†’ Creates string from Unicode.
  • fromCodePoint(code1, ...) (Static) β†’ Supports larger Unicode points.
"ha".repeat(3);                // "hahaha"
String.fromCharCode(65, 66);  // "AB"
Enter fullscreen mode Exit fullscreen mode

πŸ” Comparison & Value

  • localeCompare(other) β†’ Compares two strings (for sorting).
  • valueOf() β†’ Returns primitive string value.
  • toString() β†’ Converts value to string.
"apple".localeCompare("banana"); // -1 (means "apple" < "banana")
Enter fullscreen mode Exit fullscreen mode

πŸ“š Summary Table

Method Description
length Get string length
toUpperCase() Convert to uppercase
toLowerCase() Convert to lowercase
slice() Extract part of a string
substring() Extract part (no negative index)
substr() Extract part (deprecated)
indexOf() First index of a match
lastIndexOf() Last index of a match
includes() Check if contains substring
startsWith() Check if starts with value
endsWith() Check if ends with value
search() Search using regex
match() Regex match
matchAll() All regex matches
trim() Remove both side whitespaces
trimStart() Remove start whitespace
trimEnd() Remove end whitespace
replace() Replace first occurrence
replaceAll() Replace all occurrences
split() Split string into array
concat() Join strings
charAt() Get character at index
charCodeAt() Unicode of character
repeat() Repeat string
fromCharCode() Unicode to character
localeCompare() Compare strings

Top comments (0)