This loop came into the ES6. It is basically used to get the iteration value from the Array.
Syntax:-
for (variable of iterable) {
// logic
}
Example:-
var arr=[10,20,30];
for (var element of arr){
console.log(element);
}
Output:- 10 20 30
Note:- you can use var, let and const to get the iteration value.
get the iteration value from the string
var data="John";
for( let element of data){
console.log(element);
}
Output:- J
o
h
n
o
h
n
get the iteration value from the Array Object
var data=[{name:"John", age:35},{name:"Rom", age:30}]
for( var element of data){
console.log("My name is "+element.name+" and age is "+element.age);
}
Output:-
My name is John and age is 35
My name is Rom and age is 30
My name is John and age is 35
My name is Rom and age is 30