Languages
[Edit]
EN

JavaScript - iterate through string

3 points
Created by:
Elmer-Li
396

In this article, we would like to show you how to iterate through string characters in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

let text = 'ABCD';

for (let i = 0; i < text.length; i++) {
  console.log(text.charAt(i));
}

 

Alternative solution

In this example, we use for...of loop to iterate through each character of the text string.

// ONLINE-RUNNER:browser;

const text = 'ABCD';

for (const character of text) {
  console.log(character);
}

Output:

A
B
C
D

References

JavaScript - String (popular problems)

JavaScript - iterate through string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join