TExcelTAD
Home › JavaScript › JS String Methods

JavaScript String Methods

Strings come with dozens of built-in methods for changing case, trimming, cutting out parts, replacing text, splitting and padding. Because strings are immutable, every method returns a new string and leaves the original untouched.

Changing case and trimming

js · try it yourself
const raw = "   Hello World   ";
console.log(raw.toUpperCase());
console.log(raw.toLowerCase());
console.log(`[${raw.trim()}]`);
console.log(`[${raw.trimStart()}]`);
console.log(`[${raw.trimEnd()}]`);
   HELLO WORLD   
   hello world   
[Hello World]
[Hello World   ]
[   Hello World]

trim() is a must for user input — people often type stray spaces.

Extracting parts: slice, substring, at

slice(start, end) returns the characters from start up to — but not including — end. Negative positions count from the end:

js · try it yourself
const text = "JavaScript rocks";
console.log(text.slice(0, 4));    // positions 0..3
console.log(text.slice(4, 10));
console.log(text.slice(11));      // to the end
console.log(text.slice(-5));      // last five
console.log(text.slice(-5, -2));
console.log(text.substring(4, 0)); // substring swaps reversed arguments
console.log(text.at(-1), text.charAt(0));
Java
Script
rocks
rocks
roc
Java
s J
Method Negative indexes Notes
slice(start, end) ✅ count from end the one to remember
substring(start, end) ❌ treated as 0 swaps arguments if start > end
at(i) ✅ single character
charAt(i) ❌ returns "" when out of range

Replacing text

replace changes the first match; replaceAll changes every match. Both are case-sensitive:

js · try it yourself
const msg = "Cats are great. Cats are fluffy.";
console.log(msg.replace("Cats", "Dogs"));
console.log(msg.replaceAll("Cats", "Dogs"));
console.log(msg.replace(/cats/gi, "Birds"));   // regex: global + ignore case
Dogs are great. Cats are fluffy.
Dogs are great. Dogs are fluffy.
Birds are great. Birds are fluffy.

Splitting and joining

split(separator) breaks a string into an array; the array method join glues it back together:

js · try it yourself
const csv = "red,green,blue";
const colors = csv.split(",");
console.log(colors);
console.log(colors.join(" | "));
console.log("hello".split(""));          // into characters
console.log("one two  three".split(" "));
[ 'red', 'green', 'blue' ]
red | green | blue
[ 'h', 'e', 'l', 'l', 'o' ]
[ 'one', 'two', '', 'three' ]

Notice the empty string in the last result — two spaces in a row produce an empty piece between them.

Padding, repeating and concatenating

js · try it yourself
const id = "42";
console.log(id.padStart(5, "0"));       // 00042
console.log("Total".padEnd(10, ".") + "9.99");
console.log("ab".repeat(3));
console.log("-".repeat(20));
console.log("Java".concat("Script", "!"));
00042
Total.....9.99
ababab
--------------------
JavaScript!

padStart and padEnd are perfect for aligning columns in text output.

Chaining methods

Since each method returns a new string, you can call methods one after another:

js · try it yourself
const input = "  john.SMITH@Example.COM ";
const email = input.trim().toLowerCase();
const [user, domain] = email.split("@");
const displayName = user
  .split(".")
  .map((part) => part[0].toUpperCase() + part.slice(1))
  .join(" ");
console.log(email);
console.log(displayName, "at", domain);
john.smith@example.com
John Smith at example.com

Quick reference

Method Returns
toUpperCase() / toLowerCase() case-changed copy
trim() / trimStart() / trimEnd() copy without surrounding white space
slice(a, b) / substring(a, b) part of the string
replace(x, y) / replaceAll(x, y) copy with replacements
split(sep) array of pieces
padStart(n, s) / padEnd(n, s) copy padded to length n
repeat(n) copy repeated n times
concat(...s) joined string

Searching methods (indexOf, includes, startsWith…) are covered in the next chapter.

Test yourself

What does "Hello".slice(1, 3) return?

  1. "Hel"
  2. "el"
  3. "ell"
  4. "ello"

What does "a-b-c".replace("-", "+") return?

  1. "a+b+c"
  2. "a+b-c"
  3. "a-b+c"
  4. "abc"

What is "7".padStart(3, "0")?

  1. "700"
  2. "007"
  3. "0007"
  4. "7"