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.

Length, charAt(), and at()
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).

slice() and substring()
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().

Case and Trim Methods
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(), padEnd(), repeat(), concat()
// 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.

replace(), replaceAll(), split()
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(""));
MethodDescriptionReturns
lengthNumber of charactersNumber
charAt(i)Character at index iString
at(i)Character at index (supports negative)String
slice(s, e)Extract from s to eString
substring(s, e)Extract from s to e (no negatives)String
toUpperCase()Convert to uppercaseString
toLowerCase()Convert to lowercaseString
trim()Remove whitespace from both endsString
padStart(n, s)Pad from start to length nString
padEnd(n, s)Pad from end to length nString
repeat(n)Repeat string n timesString
replace(a, b)Replace first match of a with bString
replaceAll(a, b)Replace all matches of a with bString
split(sep)Split into array by separatorArray
concat(s)Join strings togetherString
📝 Note: Strings are immutable in JavaScript. Every string method returns a new string and never modifies the original. Always assign the result to a variable if you need the changed value.
Exercise:
Which method is used to extract a section of a string and supports negative indexes?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.