Explore JavaScript string methods for searching, extracting, transforming, and manipulating text data.
Strings in JavaScript are immutable — once created, a string value cannot be changed. Any method that appears to modify a string actually returns a brand new string.
You can create strings using single quotes, double quotes, or backticks (template literals):
const single = 'Hello';
const double = "Hello";
const template = `Hello, ${name}!`; // template literal with interpolationEvery string has a length property:
const greeting = 'Hello';
console.log(greeting.length); // 5You can access individual characters using bracket notation or the charAt() method:
const word = 'JavaScript';
console.log(word[0]); // 'J'
console.log(word.charAt(4)); // 'S'
console.log(word[word.length - 1]); // 't' (last character)Strings are also iterable, which means you can loop over them with a for...of loop:
for (const char of 'Hello') {
console.log(char); // 'H', 'e', 'l', 'l', 'o'
}JavaScript provides several methods for finding content within a string.
indexOf(substring) returns the index of the first occurrence, or -1 if not found:
const text = 'Hello, World!';
console.log(text.indexOf('World')); // 7
console.log(text.indexOf('xyz')); // -1lastIndexOf(substring) works the same but searches from the end:
const path = 'folder/subfolder/file.txt';
console.log(path.lastIndexOf('/')); // 16includes(substring) returns a boolean — simpler when you only need to know if the substring exists:
console.log('JavaScript'.includes('Script')); // true
console.log('JavaScript'.includes('script')); // false (case-sensitive)startsWith() and endsWith() check the beginning or end of a string:
const filename = 'report.pdf';
console.log(filename.startsWith('report')); // true
console.log(filename.endsWith('.pdf')); // truesearch(regex) works like indexOf but accepts a regular expression:
const text = 'Hello World 123';
console.log(text.search(/\d+/)); // 12 (index where digits start)Extracting substrings:
slice(start, end) extracts a section of a string. The end index is exclusive. Negative indices count from the end:
const str = 'JavaScript';
console.log(str.slice(0, 4)); // 'Java'
console.log(str.slice(4)); // 'Script'
console.log(str.slice(-6)); // 'Script'substring(start, end) is similar to slice but does not support negative indices.
Changing case:
console.log('hello'.toUpperCase()); // 'HELLO'
console.log('HELLO'.toLowerCase()); // 'hello'Removing whitespace:
const padded = ' Hello ';
console.log(padded.trim()); // 'Hello'
console.log(padded.trimStart()); // 'Hello '
console.log(padded.trimEnd()); // ' Hello'Padding strings:
console.log('5'.padStart(3, '0')); // '005'
console.log('Hi'.padEnd(10, '.')); // 'Hi........'Repeating:
console.log('ha'.repeat(3)); // 'hahaha'split(separator) divides a string into an array of substrings:
const csv = 'apple,banana,cherry';
const fruits = csv.split(',');
console.log(fruits); // ['apple', 'banana', 'cherry']
const words = 'Hello World'.split(' ');
console.log(words); // ['Hello', 'World']Array.join(separator) is the reverse — it combines array elements into a string:
const parts = ['2024', '01', '15'];
console.log(parts.join('-')); // '2024-01-15'replace(search, replacement) replaces the first occurrence:
const text = 'Hello World World';
console.log(text.replace('World', 'JS')); // 'Hello JS World'replaceAll(search, replacement) replaces all occurrences:
console.log(text.replaceAll('World', 'JS')); // 'Hello JS JS'These methods are frequently chained together for powerful text transformations:
const input = ' hello, world! ';
const result = input.trim().toUpperCase().replace('WORLD', 'JS');
console.log(result); // 'HELLO, JS!'<div id="output"></div>
<script>
const original = " JavaScript is Awesome! ";
// Trimming and transforming
const trimmed = original.trim();
const upper = trimmed.toUpperCase();
const replaced = upper.replace('AWESOME', 'POWERFUL');
// Searching
const hasJS = trimmed.includes('JavaScript');
const position = trimmed.indexOf('is');
// Splitting and joining
const words = trimmed.split(' ');
const reversed = words.reverse().join(' ');
const output = document.getElementById('output');
output.innerHTML = `
<p>Original: "${original}"</p>
<p>Trimmed: "${trimmed}"</p>
<p>Uppercase: "${upper}"</p>
<p>Replaced: "${replaced}"</p>
<p>Includes "JavaScript": ${hasJS}</p>
<p>Index of "is": ${position}</p>
<p>Words reversed: "${reversed}"</p>
`;
</script>What does 'hello'.indexOf('z') return?