How to Iterate Over Characters of a String in JavaScript ?
There are several methods to iterate over characters of a string in JavaScript.
1. Using for Loop
The classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.
Syntax
for (statement 1 ; statement 2 ; statement 3){
code here...
};
let str = "Hello";
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
Output
H e l l o
2. Using for...of Loop
The for...of loop is a modern way to iterate directly over the characters of a string without needing to use indexing.
Syntax
for ( variable of iterableObjectName) {
// Code...
}
let str = "Hello";
for (let char of str) {
console.log(char);
}
Output
H e l l o
3. Using forEach() Method
The forEach() method can be used on arrays, so we first split the string into an array of characters, then use forEach() to iterate.
Syntax
array.forEach(callback(element, index, arr), thisValue)
let str = "Hello";
str.split('').forEach((char, index) => {
console.log(`${index}: ${char}`);
});
Output
0: H 1: e 2: l 3: l 4: o
4. Using charAt() Method with while Loop
The charAt() method returns the character at a given index. Combining it with a while loop allows us to iterate over the string by manually tracking the index.
Syntax
let index = 0;
while (index < str.length) {
let char = str.charAt(index);
// code here...
index++;
}
let str = "Hello";
let index = 0;
while (index < str.length) {
console.log(str.charAt(index));
index++;
}
Output
H e l l o
5. Using reduce() Method
The reduce() method can be used to iterate over a string by first splitting it into an array, then using the accumulator to concatenate or perform operations on each character.
Syntax:
string.split('').reduce((acc, char) =>
{
// Process char return acc + char;}, ''
);
let str = "Hello";
let result = str.split('').reduce((acc, char) => acc + char, '');
console.log(result); // Outputs: "Hello"
Output
Hello
6. Using for...in Loop
The for...in loop allows us to iterate over the indices of the string, which we can then use to access each character.
Syntax
for (let index in str) {
const char = str[index];
// code here...
}
let str = "Hello";
for (let index in str) {
console.log(str[index]);
}
Output
H e l l o