JavaScript Code


How to iterate over characters of a string in JavaScript?


Iterate over characters of a string

Given a string, iterate over the characters of the given string, using JavaScript.

Solution

To iterate over the characters of a given string using JavaScript, split the string into array of characters, and then use a looping statement to iterate over the elements of the array which are characters.

Program

1. Given a string str . Iterate over the characters of string str using for loop.

var str = 'apple';

var chars = str.split("");

for ( let i = 0; i < chars.length; i++ ) {
    console.log(chars[i]);
}