Technology · JavaScript

JavaScript String Methods

Master string manipulation with substring, slice, replace, split, and common text operations.

TL;DR
  1. 01Use slice, substring, or substr to extract parts of strings.
  2. 02Use replace and replaceAll to change text inside strings.
  3. 03Use split and join to convert between strings and arrays.

Extracting Substrings

  • Use slice() to extract a portion of a string by start and end index.
    const text = "Hello World";
    text.slice(0, 5); // "Hello"
    text.slice(-5);   // "World"
  • Use substring() for similar behavior but treating negative numbers as zero.
    "Hello".substring(1, 4); // "ell"
  • Use substr() with length instead of an end index, but avoid it.
    "Hello".substr(1, 3); // "ell"
  • Slice is preferred over substring and substr for modern code.
  • Negative indices in slice count from the end of the string.

Finding and Checking

  • Use indexOf() to find the first position of a substring.
    "hello world".indexOf("world"); // 6
    "hello world".indexOf("xyz");   // -1
  • Use includes() for a simple boolean check without the index.
    "hello world".includes("world"); // true
  • Use startsWith() and endsWith() to check string boundaries.
    "hello".startsWith("he");   // true
    "hello".endsWith("lo");     // true
  • Use lastIndexOf() to find the last occurrence of a substring.
  • Use search() with a regex to find the index of the first pattern match.
    "hello123".search(/\d+/); // 5 — index where digits start

Replace and Transform

  • Use replace() to replace the first occurrence of a substring.
    "hello world".replace("world", "there"); // "hello there"
  • Use replaceAll() to replace every occurrence without a loop.
    "hello hello".replaceAll("hello", "hi"); // "hi hi"
  • Use a regex for advanced replacements and pattern matching.
    "hello123".replace(/[0-9]/g, "X"); // "helloXXX"
  • Use toUpperCase() and toLowerCase() for case conversion.
    "Hello".toUpperCase(); // "HELLO"
  • Use a function as the replace callback to compute each replacement.
    "hello world".replace(/\b\w/g, (ch) => ch.toUpperCase());
    // "Hello World"

Split and Join

  • Use split() to break a string into an array by a delimiter.
    "a,b,c".split(","); // ["a", "b", "c"]
    "hello".split("");   // ["h", "e", "l", "l", "o"]
  • Use join() to combine an array back into a string.
    ["a", "b", "c"].join(","); // "a,b,c"
  • Combine split and join for text transformations like removing spaces.
    "h e l l o".split(" ").join(""); // "hello"
  • Pass a limit as the second argument to split for partial results.
    "a,b,c,d".split(",", 2); // ["a", "b"]
  • Split by regex to handle multiple delimiters at once.
    "one, two; three".split(/[,;]\s*/); // ["one", "two", "three"]

Trimming and Padding

  • Use trim() to remove whitespace from both ends of a string.
    "  hello  ".trim(); // "hello"
  • Use trimStart() or trimLeft() to remove only leading whitespace.
    "  hello".trimStart(); // "hello"
  • Use trimEnd() or trimRight() for trailing whitespace only.
    "hello  ".trimEnd(); // "hello"
  • Use padStart() and padEnd() to add characters for fixed-width strings.
    "5".padStart(3, "0");  // "005"
    "5".padEnd(3, "0");    // "500"
  • Use repeat() to create a string repeated a given number of times.
    "-".repeat(10); // "----------"

Tip: Use includes() instead of indexOf() !== -1 for cleaner boolean checks on string presence.

Warning: substr() is deprecated, so use slice() or substring() instead for extracting portions of strings.

JavaScript Set and MapJavaScript Template Literals