JavaScript String Methods: A Complete Guide for Beginners
- Introduction to JavaScript Methods
- List of JavaScript String Methods with Examples
- 1 Length of a String
- 2 Changing the Case of a String
- 3 Extracting a Part of a String
- 4 Searching in a String
- 5 Replacing Content in a String
- 6 Splitting a String
- 7 Trimming Spaces
- 8 Repeating a String
- 9 Getting Characters from a String
- 10 Concatenating Strings
- 11 Padding a String Adding Extra Characters
- 12 Checking If a String Matches a Pattern
- 13 Extracting a Part of a String with Template Literals
- Summary Table of String Methods
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 |