Introduction
JavaScript provides a rich set of **string methods** to manipulate, search, and format text. 📝 In this tutorial, we cover the most important string methods with examples and best practices.
1️⃣ search()
Searches a string for a pattern and returns the index of the match.
Code Snippet
const str = "Hello World";
console.log(str.search("World")); // 62️⃣ indexOf()
Returns the first index of a substring, or -1 if not found.
Code Snippet
const str = "Hello World";
console.log(str.indexOf("o")); // 43️⃣ lastIndexOf()
Returns the last occurrence index of a substring.
Code Snippet
const str = "Hello World";
console.log(str.lastIndexOf("o")); // 74️⃣ includes()
Checks if a string contains a substring and returns true/false.
Code Snippet
const str = "Hello World";
console.log(str.includes("World")); // true5️⃣ startsWith()
Checks if the string starts with a specified substring.
Code Snippet
const str = "Hello World";
console.log(str.startsWith("Hello")); // true6️⃣ endsWith()
Checks if the string ends with a specified substring.
Code Snippet
const str = "Hello World";
console.log(str.endsWith("World")); // true7️⃣ match()
Searches a string using a regular expression and returns matches.
Code Snippet
const str = "I love JavaScript";
console.log(str.match(/love/)); // ["love"]8️⃣ trim()
Removes whitespace from both ends of a string.
Code Snippet
const str = " Hello ";
console.log(str.trim()); // "Hello"9️⃣ trimStart()
Removes whitespace from the start of a string.
Code Snippet
const str = " Hello ";
console.log(str.trimStart()); // "Hello "🔟 trimEnd()
Removes whitespace from the end of a string.
Code Snippet
const str = " Hello ";
console.log(str.trimEnd()); // " Hello"1️⃣1️⃣ padStart()
Adds padding to the start of a string until it reaches a specified length.
Code Snippet
const str = "5";
console.log(str.padStart(3, "0")); // "005"1️⃣2️⃣ padEnd()
Adds padding to the end of a string until it reaches a specified length.
Code Snippet
const str = "5";
console.log(str.padEnd(3, "0")); // "500"1️⃣3️⃣ split()
Splits a string into an array based on a separator.
Code Snippet
const str = "a,b,c";
console.log(str.split(",")); // ["a","b","c"]1️⃣4️⃣ slice()
Extracts a substring based on start and end indices.
Code Snippet
const str = "Hello World";
console.log(str.slice(0,5)); // "Hello"1️⃣5️⃣ concat()
Joins two or more strings together.
Code Snippet
const str1 = "Hello";
const str2 = "World";
console.log(str1.concat(" ", str2)); // "Hello World"1️⃣6️⃣ Template Literals
Enables embedding expressions inside strings using backticks \` \`.
Code Snippet
const name = "Alice";
console.log(`Hello, ${name}!`); // "Hello, Alice!"1️⃣7️⃣ replaceAll()
Replaces all occurrences of a substring with a new string.
Code Snippet
const str = "foo bar foo";
console.log(str.replaceAll("foo", "baz")); // "baz bar baz"1️⃣8️⃣ toUpperCase()
Converts the string to uppercase.
Code Snippet
const str = "hello";
console.log(str.toUpperCase()); // "HELLO"1️⃣9️⃣ toLowerCase()
Converts the string to lowercase.
Code Snippet
const str = "HELLO";
console.log(str.toLowerCase()); // "hello"🔥 Summary
JavaScript string methods provide powerful ways to **search, manipulate, format, and transform text**. By mastering these methods, you can handle most text processing tasks efficiently and write cleaner, more readable code. 💡