To iterate over characters in a string in JavaScript, you can use a for loop or the forEach method of strings.
In this example,
str
with a value of "JavaScript"
.i
to iterate over the string length.str[i]
.let str = 'JavaScript';
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
J a v a S c r i p t
In this example,
str
with a value of "TypeScript"
.split('')
method on the string to convert it into an array of characters.forEach
method on the array to iterate over each character.let str = 'TypeScript';
str.split('').forEach(char => {
console.log(char);
});
T y p e S c r i p t
In this tutorial, we learned How to Iterate over Characters in String in JavaScript language with well detailed examples.