Loops and Text
In this lesson, we will look at iterating through text using loops in JavaScript. As we mentioned in the lesson about arrays, text in JavaScript is an array of characters, which means we can iterate through each character in the text using any loop.
Example: We want to iterate through each character in the word 'Test' and print it:
const word = "Test";
for (const character of word) {
console.log(character);
}Output:
T
e
s
tIn this example, we iterate through each character in the string word and print it. You can certainly imagine that we can achieve the same using a standard for or even a while loop.
Instructions
Choose a loop to iterate through the variable text and print each character on a new line.
Start programming for free
7/9