Sometimes, we want to loop through a string with JavaScript.
In this article, we’ll look at how to loop through a string with JavaScript.
How to loop through a string with JavaScript?
To loop through a string with JavaScript, we can use the for-of loop.
For instance, we write:
const str = '123456';
for (const s of str) {
console.log(s)
}
to loop through each character of str
with the for-of loop.
s
is the character of str
being looped through.
Therefore, we see:
"1"
"2"
"3"
"4"
"5"
"6"
logged.
Conclusion
To loop through a string with JavaScript, we can use the for-of loop.