Skip to main content

JavaScript: for...of Loop

For of is another alternative to for in, which is more accessible than for in. Here it fetches and returns the value of each element in an array.

Syntax

for(variable of object)
{
    //condition
}

Example:

let infinityStones = ['Soul', 'Time', 'Reality', 'Power', 'Mind', 'Space']

for(let stones of infinityStones)
{
    console.log(stones+ " Stone ")
}

Output:

Soul Stone
Time Stone
Reality Stone
Power Stone 
Mind Stone
Space Stone

As you can see here, it caught the element's value directly after iterating through the infinityStones.

Tags
Submitted by arilio666 on October 6, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.