ES6 - for in loop



The for...in loop is used to loop through an object's properties.

Following is the syntax of ‘for…in’ loop.

for (variablename in object) {
   statement or block to execute
}

In each iteration, one property from the object is assigned to the variable name and this loop continues till all the properties of the object are exhausted.

Example

var obj = {a:1, b:2, c:3};

for (var prop in obj) {
   console.log(obj[prop]);
}

The above example illustrates iterating an object using the for... in loop. The following output is displayed on successful execution of the code.

1
2
3
Advertisements