How to Iterate Over Characters of a String in JavaScript ?
Last Updated :
23 Jul, 2025
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...
};
JavaScript
let str = "Hello";
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
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...
}
JavaScript
let str = "Hello";
for (let char of str) {
console.log(char);
}
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)
JavaScript
let str = "Hello";
str.split('').forEach((char, index) => {
console.log(`${index}: ${char}`);
});
Output0: 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++;
}
JavaScript
let str = "Hello";
let index = 0;
while (index < str.length) {
console.log(str.charAt(index));
index++;
}
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;}, ''
);
JavaScript
let str = "Hello";
let result = str.split('').reduce((acc, char) => acc + char, '');
console.log(result); // Outputs: "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...
}
JavaScript
let str = "Hello";
for (let index in str) {
console.log(str[index]);
}
Iterating over String in JavaScript
How to Iterate Over Characters of a String in JavaScript ?
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics