Why do I have to use for(word of storyWord)? Is word built-in javascript method?

Hello, I am a Javascript beginner. There is a question I wanted to ask. In the Mini Linter project, we use:

for(word of storyWords)

Here “word” in for loop built-in method? And what if I try to make it a variable, would it take my variable word or the built-in word method? Or it’ll pass me an error?

The loop would normally be written with let, to confine the variable to the code block of the loop.

for (let word of storyWords) {

}

To answer your question, word is an arbitrary name that best describes what the variable refers to, a word among words. It is not built in, but chosen by the author. Any variable name would alright.

for (let w of storyWords) {

}

So then “of” refers “word” variable inside the “storyWords” variable?

If the storyWords an array is and content [‘element1’, ‘element2’, ‘element3’] is then the “word” behaves like an index wariable in looping that takes first ‘element1’ value then ‘element2’ value then ‘element3’ value and executes the statements in curly brackets on it’s. Is similar to for…i loop, but you dont need to know how many element is in the storyWords array.

I thought that was at the core, but had to feel it out. The, of keyword is actually an iterator since it causes the traversal of the complete array, value by value.

Thanks now I understand!

1 Like