Languages
[Edit]
EN

JavaScript - for...in statement

0 points
Created by:
Welsh
772

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

References

  1. for...in - JavaScript | MDN

Alternative titles

  1. JavaScript - for in loop
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - Statements & declarations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join