EN
JavaScript - for...in statement
0
points
In this article, we would like to show you how to use for...in statement in JavaScript.
Description
The for...in
loop is used to iterate over enumerable and non-Symbol properties.
Syntax
for (variable in object) {
// statement
}
Practical examples
In this example, we iterate over an object to log its keys/values in the console.
// ONLINE-RUNNER:browser;
var object = {
key1: {
name: 'John',
age: 43,
},
key2: {
name: 'Kate',
age: 54,
},
key3: {
name: 'Diego',
age: 21,
},
};
for (var key in object) {
if (object.hasOwnProperty(key)) {
var entry = object[key];
console.log(key + ' : ' + entry.name + ', ' + entry.age);
}
}
Output:
key1 : John, 43
key2 : Kate, 54
key3 : Diego, 21