JavaScript String Methods
JavaScript strings come with a rich set of built-in methods for manipulating text. Strings are immutable, so all string methods return a new string without changing the original.
String Length and Character Access
The length property returns the number of characters in a string. You can access individual characters using charAt(), at(), or bracket notation.
let text = "Hello, World!";
console.log(text.length);
// charAt() returns character at a given index
console.log(text.charAt(0));
console.log(text.charAt(7));
// at() supports negative indexing (ES2022)
console.log(text.at(0));
console.log(text.at(-1));
console.log(text.at(-2));
// Bracket notation (no negative index support)
console.log(text[0]);
console.log(text[4]);Extracting String Parts
JavaScript provides slice() and substring() to extract portions of a string. Both take a start index and an optional end index (not included in the result).
let str = "Hello, World!";
// slice(start, end) — end not included
console.log(str.slice(0, 5));
console.log(str.slice(7));
// slice() with negative indexes counts from end
console.log(str.slice(-6));
console.log(str.slice(-6, -1));
// substring(start, end) — similar but no negatives
console.log(str.substring(0, 5));
console.log(str.substring(7));Case Conversion and Trimming
Convert strings to upper or lower case with toUpperCase() and toLowerCase(). Remove whitespace with trim(), trimStart(), and trimEnd().
let greeting = "Hello, World!";
console.log(greeting.toUpperCase());
console.log(greeting.toLowerCase());
let padded = " Hello! ";
console.log(padded.trim());
console.log(padded.trimStart());
console.log(padded.trimEnd());
// Useful for user input
let email = " User@Email.COM ";
let cleaned = email.trim().toLowerCase();
console.log(cleaned);Padding, Repeat, and Concat
padStart() and padEnd() pad a string to a target length. repeat() repeats a string a given number of times. concat() joins strings together.
// padStart(targetLength, padString)
let num = "5";
console.log(num.padStart(3, "0"));
console.log(num.padStart(5, "*"));
// padEnd(targetLength, padString)
let item = "Apple";
console.log(item.padEnd(10, "."));
// repeat(count)
let dash = "-";
console.log(dash.repeat(20));
let ha = "Ha";
console.log(ha.repeat(3));
// concat()
let first = "Hello";
let second = "World";
console.log(first.concat(" ", second, "!"));Replace and Split
replace() replaces the first match, replaceAll() replaces every occurrence. split() divides a string into an array based on a separator.
let text = "I love cats. Cats are great!";
// replace() — first match only (case-sensitive)
console.log(text.replace("cats", "dogs"));
// replace with regex and global flag
console.log(text.replace(/cats/gi, "dogs"));
// replaceAll() — all matches
let csv = "a,b,c,d,e";
console.log(csv.replaceAll(",", " | "));
// split() — string to array
let fruits = "apple,banana,cherry";
let arr = fruits.split(",");
console.log(arr);
console.log(arr.length);
// Split every character
let word = "Hello";
console.log(word.split(""));| Method | Description | Returns |
|---|---|---|
| length | Number of characters | Number |
| charAt(i) | Character at index i | String |
| at(i) | Character at index (supports negative) | String |
| slice(s, e) | Extract from s to e | String |
| substring(s, e) | Extract from s to e (no negatives) | String |
| toUpperCase() | Convert to uppercase | String |
| toLowerCase() | Convert to lowercase | String |
| trim() | Remove whitespace from both ends | String |
| padStart(n, s) | Pad from start to length n | String |
| padEnd(n, s) | Pad from end to length n | String |
| repeat(n) | Repeat string n times | String |
| replace(a, b) | Replace first match of a with b | String |
| replaceAll(a, b) | Replace all matches of a with b | String |
| split(sep) | Split into array by separator | Array |
| concat(s) | Join strings together | String |