J

JavaScript Handbook

Clean • Professional

String Methods

4 minute

String Methods

JavaScript strings are primitive and immutable, meaning all string methods return a new string without modifying the original. Below is a list of commonly used string methods with explanations and examples.

length

Returns the total number of characters in the string, including spaces, symbols, and emojis. Useful for validating input length or iterating through strings.

let str = "Hello";
console.log(str.length); // 5

charAt(index)

Returns the character located at the specified index. If the index is out of bounds, it returns an empty string.

let str = "Hello";
console.log(str.charAt(1)); // "e"

charCodeAt(index)

Returns the UTF-16 numeric code of the character at the given index. Useful for comparing characters or working with Unicode.

let str = "A";
console.log(str.charCodeAt(0)); // 65

codePointAt(index)

Returns the full Unicode code point of a character, supporting characters outside the BMP (like emojis).

let str = "😀";
console.log(str.codePointAt(0)); // 128512

concat()

Combines two or more strings into one. Useful when building dynamic messages or combining values.

let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // "Hello World"

at(index)

Returns the character at the given index. Supports negative indexing to count from the end of the string.

let str = "Hello";
console.log(str.at(-1)); // "o"

Bracket Notation [ ]

Access individual characters like elements of an array. Simple and widely used.

let str = "Hello";
console.log(str[0]); // "H"

slice(start, end)

Extracts a section of the string from start index to end index (exclusive). Supports negative indices.

let str = "Hello World";
console.log(str.slice(0, 5)); // "Hello"

substring(start, end)

Similar to slice(), but negative indices are treated as 0. Useful for extracting parts of a string safely.

let str = "Hello World";
console.log(str.substring(6, 11)); // "World"

substr(start, length) (deprecated)

Extracts a substring starting at start for a specific length. Avoid in new code; use slice instead.

let str = "Hello World";
console.log(str.substr(6, 5)); // "World"

toUpperCase()

Converts all letters in a string to uppercase. Helpful for standardizing text or performing case-insensitive comparisons.

let str = "hello";
console.log(str.toUpperCase()); // "HELLO"

toLowerCase()

Converts all letters in a string to lowercase.

let str = "WORLD";
console.log(str.toLowerCase()); // "world"

isWellFormed()

Checks if the string contains well-formed Unicode sequences. Returns true or false.

let str = "Hello";
console.log(str.isWellFormed()); // true

toWellFormed()

Returns a string with invalid Unicode sequences replaced, ensuring proper Unicode formatting.

let str = "\\uD800"; // Unpaired surrogate
console.log(str.toWellFormed()); // "\\uFFFD"

trim()

Removes all leading and trailing whitespace from a string. Commonly used to clean user input.

let str = "  Hello  ";
console.log(str.trim()); // "Hello"

trimStart()

Removes whitespace from the start of a string.

let str = "  Hello";
console.log(str.trimStart()); // "Hello"

trimEnd()

Removes whitespace from the end of a string.

let str = "Hello  ";
console.log(str.trimEnd()); // "Hello"

padStart(targetLength, padString)

Pads the start of the string with a specified string until the target length is reached. Useful for formatting numbers or codes.

let str = "5";
console.log(str.padStart(3, "0")); // "005"

padEnd(targetLength, padString)

Pads the end of the string to reach the target length.

let str = "5";
console.log(str.padEnd(3, "0")); // "500"

repeat(count)

Repeats the string a specified number of times. Useful for creating patterns or duplicating content.

let str = "Hi ";
console.log(str.repeat(3)); // "Hi Hi Hi "

replace(searchValue, newValue)

Replaces the first occurrence of a substring with a new value.

let str = "Hello World";
console.log(str.replace("World", "Universe")); // "Hello Universe"

replaceAll(searchValue, newValue) (ES2021+)

Replaces all occurrences of a substring in the string.

let str = "Hi Hi Hi";
console.log(str.replaceAll("Hi", "Hey")); // "Hey Hey Hey"

split(separator)

Splits a string into an array of substrings based on the specified separator.

let str = "a,b,c";
console.log(str.split(",")); // ["a", "b", "c"]

JavaScript String Methods – Quick Reference

MethodDescriptionExample
lengthReturns total characters in the string."Hello".length // 5
charAt(index)Gets character at the given index."Hello".charAt(1) // "e"
charCodeAt(index)Returns UTF-16 code of character."A".charCodeAt(0) // 65
codePointAt(index)Returns Unicode code point."😀".codePointAt(0) // 128512
concat()Combines strings."Hello".concat(" ", "World") // "Hello World"
at(index)Returns character, supports negative index."Hello".at(-1) // "o"
[ ]Access character like array element."Hello"[0] // "H"
slice(start, end)Extracts part of string."Hello World".slice(0,5) // "Hello"
substring(start, end)Extracts substring, ignores negative indices."Hello World".substring(6,11) // "World"
substr(start, length)Extracts substring by start and length (deprecated)."Hello World".substr(6,5) // "World"
toUpperCase()Converts to uppercase."hello".toUpperCase() // "HELLO"
toLowerCase()Converts to lowercase."WORLD".toLowerCase() // "world"
isWellFormed()Checks well-formed Unicode."Hello".isWellFormed() // true"
toWellFormed()Returns valid Unicode string."\\uD800".toWellFormed() // "\\uFFFD"
trim()Removes whitespace from both ends." Hello ".trim() // "Hello"
trimStart()Removes whitespace from start." Hello".trimStart() // "Hello"
trimEnd()Removes whitespace from end."Hello ".trimEnd() // "Hello"
padStart(targetLength, padString)Pads string at start."5".padStart(3,"0") // "005"
padEnd(targetLength, padString)Pads string at end."5".padEnd(3,"0") // "500"
repeat(count)Repeats string specified times."Hi ".repeat(3) // "Hi Hi Hi "
replace(search, newValue)Replaces first occurrence."Hello World".replace("World","Universe") // "Hello Universe"
replaceAll(search, newValue)Replaces all occurrences."Hi Hi Hi".replaceAll("Hi","Hey") // "Hey Hey Hey"
split(separator)Splits string into array."a,b,c".split(",") // ["a","b","c"]

 

Article 0 of 0