JavaScript String Methods: A Complete Guide for Beginners
Introduction to JavaScript Methods
Strings are one of the most commonly used data types in JavaScript. Whether you're manipulating text, searching for patterns, or formatting output, JavaScript provides powerful built-in string methods to make your work easier.
In this blog, we will explore all essential JavaScript string methods with easy explanations and examples.
List of JavaScript String Methods with Examples
JavaScript provides many built-in string methods to manipulate and work with strings. Here’s a list of all important string functions along with examples.
🔹 1. Length of a String
🔹 length
→ Returns the number of
characters in a string.
let str = "Hello World";
console.log(str.length); // Output: 11
🔹 2. Changing the Case of a String
🔹 toUpperCase()
→ Converts all
letters to uppercase.
🔹 toLowerCase()
→ Converts all
letters to lowercase.
let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
console.log(str.toLowerCase()); // Output: "hello world"
🔹 3. Extracting a Part of a String
🔹 slice(start, end)
→ Extracts
part of a string from
start to
end (not included).
🔹 substring(start, end)
→
Similar to slice()
, but does not
support negative indexes.
🔹 substr(start, length)
→
Extracts a portion based on
start index and
length.
let str = "JavaScript";
console.log(str.slice(0, 4)); // Output: "Java"
console.log(str.substring(0, 4)); // Output: "Java"
console.log(str.substr(4, 6)); // Output: "Script"
🔹 4. Searching in a String
🔹 indexOf("text")
→ Returns
the
first occurrence index of a
word/character.
🔹 lastIndexOf("text")
→
Returns the
last occurrence index of a
word/character.
🔹 includes("text")
→ Returns
true
If the word/character
exists, else false
.
🔹 startsWith("text")
→ Returns
true
if the string starts with the
given text.
🔹 endsWith("text")
→ Returns
true
if the string ends with the
given text.
let str = "Hello JavaScript World";
console.log(str.indexOf("JavaScript")); // Output: 6
console.log(str.lastIndexOf("o")); // Output: 19
console.log(str.includes("World")); // Output: true
console.log(str.startsWith("Hello")); // Output: true
console.log(str.endsWith("World")); // Output: true
🔹 5. Replacing Content in a String
🔹 replace("old", "new")
→
Replaces first occurrence
of a word.
🔹 replaceAll("old", "new")
→
Replaces all occurrences of
a word.
let str = "I love JavaScript. JavaScript is awesome!";
console.log(str.replace("JavaScript", "Python"));
// Output: "I love Python. JavaScript is awesome!"
console.log(str.replaceAll("JavaScript", "Python"));
// Output: "I love Python. Python is awesome!"
🔹 6. Splitting a String
🔹 split("separator")
→ Splits
a string into an array based on the given separator.
let str = "apple,banana,grape";
console.log(str.split(",")); // Output: ["apple", "banana", "grape"]
let sentence = "I love JavaScript";
console.log(sentence.split(" ")); // Output: ["I", "love", "JavaScript"]
🔹 7. Trimming Spaces
🔹 trim()
→ Removes spaces from
both sides of a string.
🔹 trimStart()
→ Removes spaces
from the beginning.
🔹 trimEnd()
→ Removes spaces
from the end.
let str = " Hello World ";
console.log(str.trim()); // Output: "Hello World"
console.log(str.trimStart()); // Output: "Hello World "
console.log(str.trimEnd()); // Output: " Hello World"
🔹 8. Repeating a String
🔹 repeat(n)
→ Repeats a string
n
times.
let str = "Hello ";
console.log(str.repeat(3)); // Output: "Hello Hello Hello "
🔹 9. Getting Characters from a String
🔹 charAt(index)
→ Returns the
character at a specific index.
🔹 charCodeAt(index)
→ Returns
the Unicode of the character at a specific index.
🔹 at(index)
→ Returns the
character (supports negative indexing).
let str = "JavaScript";
console.log(str.charAt(2)); // Output: "v"
console.log(str.charCodeAt(2)); // Output: 118 (Unicode of 'v')
console.log(str.at(-1)); // Output: "t"
🔹 10. Concatenating Strings
🔹 concat(str1, str2, ...)
→
Joins two or more strings.
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"
🔹 11. Padding a String (Adding Extra Characters)
🔹 padStart(length, "char")
→
Adds characters at the start to make the string a fixed length.
🔹 padEnd(length, "char")
→
Adds characters at the end to make the string a fixed length.
let str = "42";
console.log(str.padStart(5, "0")); // Output: "00042"
console.log(str.padEnd(5, "x")); // Output: "42xxx"
🔹 12. Checking If a String Matches a Pattern
🔹 match(regex)
→ Finds all
matches based on a regex pattern.
🔹 matchAll(regex)
→ Finds all
matches with details.
let str = "I have 2 apples and 3 bananas";
console.log(str.match(/\d+/g)); // Output: ["2", "3"]
🔹 13. Extracting a Part of a String with Template Literals
🔹 Template Literals → Use backticks (`) to insert variables easily.
let name = "John";
let age = 25;
console.log(`My name is ${name} and I am ${age} years old.`);
🔥 Summary Table of String Methods
Method | Description |
---|---|
length |
Returns the length of a string |
toUpperCase() |
Converts to uppercase |
toLowerCase() |
Converts to lowercase |
slice(start, end)
|
Extracts part of a string |
substring(start, end)
|
Extracts part of a string |
substr(start, length)
|
Extracts with length |
indexOf("text") |
Finds first occurrence |
lastIndexOf("text")
|
Finds last occurrence |
includes("text") |
Checks if text exists |
startsWith("text")
|
Checks start |
endsWith("text") |
Checks end |
replace("old", "new")
|
Replaces text |
replaceAll("old", "new")
|
Replaces all occurrences |
split("separator")
|
Splits string into an array |
trim() |
Removes spaces |
repeat(n) |
Repeats string |
charAt(index) |
Returns character at index |
concat(str1, str2)
|
Joins strings |
padStart(length, "char")
|
Pads at start |
padEnd(length, "char")
|
Pads at end |
match(regex) |
Finds matches |