About Lesson
In this JavaScript lesson we are going to learn about JavaScript For of Loop, so for .. of is an enhanced version of for .. in and forEach loops, and it is a feature in JavaScript ES6.
Now let’s create an example, if you see in this example we have an array of names, now we want to iterate over this array, for iterating we need to use for loop, and you will see the result in the output.
1 2 3 4 5 |
const names = ["Parwiz", "John", "Ahmad", "Karim"] for(let i = 0; i < names.length; i++) { console.log(names[i]) } |
But in ES6 JavaScript we have a shorter version of for loop that is called for of loop.
1 2 3 4 5 6 |
const names = ["Parwiz", "John", "Ahmad", "Karim"] for(let name of names) { console.log(name) } |
Also you can get the index of an array by using entries() function of for of loop.
1 2 3 4 5 6 |
const names = ["Parwiz", "John", "Ahmad", "Karim"] for(let name of names.entries()) { console.log(name) } |
Exercise Files
No Attachment Found