JavaScript String Methods
JavaScript string methods cheat sheet covering slice, replace, split, trim, template literals, and text manipulation with examples.
Other JavaScript Sheets
Sign in to mark items as known and track your progress.
Sign inCase Manipulation
Transform string case for formatting and comparison
Changing Case
Convert strings to uppercase, lowercase, or capitalize for consistent formatting
'hello'.toUpperCase(); // 'HELLO'
'HELLO'.toLowerCase(); // 'hello'
// Locale-specific
'istanbul'.toLocaleUpperCase('tr-TR'); // 'İSTANBUL'Searching & Testing
Find and test for substrings and patterns within strings
Finding Substrings
Search for text within strings using indexOf, includes, and search methods
str.indexOf('world'); // 6 (first occurrence)
str.lastIndexOf('o'); // 7 (last occurrence)
str.includes('Hello'); // true
str.startsWith('Hello'); // true
str.endsWith('!'); // false
str.search(/\d+/); // 5 (regex search)Accessing Characters
Access individual characters and their codes using charAt, charCodeAt, and bracket notation
str[0]; // 'H'
str.charAt(0); // 'H'
str.charCodeAt(0); // 72 (Unicode)
str.codePointAt(0); // 72 (Unicode code point)
String.fromCharCode(72); // 'H'Start & End Checking
Check if strings start or end with specific substrings
// Check string start
const str = 'JavaScript is awesome'
console.log(str.startsWith('Java')) // true
console.log(str.startsWith('Script', 4)) // true (start from index 4)
// Check string end
console.log(str.endsWith('awesome')) // true
console.log(str.endsWith('is', 13)) // true (check up to position 13)Extracting Substrings
Extract portions of strings using various methods
Substring Methods
Extract portions of strings using slice, substring, and substr methods
str.substring(0, 5); // 'Hello'
str.substr(6, 5); // 'World' (deprecated)
str.slice(0, 5); // 'Hello'
str.slice(-5); // last 5 chars
// Differences:
// slice: allows negative indices
// substring: swaps args if start > end
// substr: second arg is length (deprecated)Modern Character Access
Access characters using the modern at() method with negative indexing
// at() method - supports negative indices
const str = 'Hello World'
console.log(str.at(0)) // 'H' (first character)
console.log(str.at(-1)) // 'd' (last character)
console.log(str.at(-6)) // ' ' (6th from end)String Modification
Modify strings through replacement, trimming, and splitting
Replacing Text
Replace text patterns using replace and replaceAll with strings or regex
str.replace('old', 'new'); // Replace first
str.replace(/old/g, 'new'); // Replace all (regex)
str.replaceAll('old', 'new'); // Replace all (ES2021)
// With function
str.replace(/\d+/g, n => n * 2); // Transform matchesTrimming & Padding
Remove whitespace with trim methods or add padding to reach desired length
' hello '.trim(); // 'hello'
' hello '.trimStart(); // 'hello '
' hello '.trimEnd(); // ' hello'
'5'.padStart(3, '0'); // '005'
'5'.padEnd(3, '0'); // '500'Splitting & Joining
Convert between strings and arrays using split and join methods
'a,b,c'.split(','); // ['a', 'b', 'c']
'hello'.split(''); // ['h', 'e', 'l', 'l', 'o']
['a', 'b'].join('-'); // 'a-b'
// With limit
'a,b,c,d'.split(',', 2); // ['a', 'b']String Formatting
Format strings with repetition, padding, and localization
Repeat & Concatenation
Repeat strings multiple times or combine them using various methods
'ab'.repeat(3); // 'ababab'
'Hello' + ' ' + 'World'; // 'Hello World'
'Hello'.concat(' ', 'World'); // 'Hello World'
`${str1} ${str2}`; // Template literalNormalization & Locale
Handle Unicode normalization and locale-specific string operations
// Unicode normalization
'é'.normalize('NFC'); // Composed form
'é'.normalize('NFD'); // Decomposed form
// Locale comparison
'ä'.localeCompare('z', 'en'); // -1
'ä'.localeCompare('z', 'sv'); // 1 (Swedish)Template Literals & String.raw
Use template literals for string interpolation and String.raw for raw strings
// Template literals
const name = 'World'
const greeting = `Hello, ${name}!` // 'Hello, World!'
// Multiline strings
const html = `
<div>
<h1>${name}</h1>
</div>
`
// String.raw - no escape processing
const path = String.raw`C:\Users\Documents` // Preserves backslashesRegular Expressions
Use regex patterns for advanced string operations
Regex String Methods
Use regular expressions with match, matchAll, and search for pattern matching
str.match(/pattern/g); // Find all matches
str.search(/pattern/); // Find index
str.replace(/old/g, 'new'); // Replace matches
str.split(/delimiter/); // Split by pattern
'test@email.com'.match(/^[^@]+@[^@]+$/); // Email validation