ES6 - for in statement

ES6 JavaScript

← Prev

In this ES6 tutorial we are going to learn about for...in statement.

What is for...in statement?

The for...in statement iterates over all enumerable string properties of an object.

Let's say we have an object with few fields and we want to iterate over the properties of the object and print the values.

const user = {
  username: 'yusuf',
  isOnline: true,
  score: 10
};

const keys = Object.keys(user);
for(let i = 0; i < keys.length; i++) {
  console.log(`key=${keys[i]} value=${user[keys[i]]}`);
}

Another way to solve this is by using for...in statement.

const user = {
  username: 'yusuf',
  isOnline: true,
  score: 10
};

for(const key in user) {
  console.log(`key=${key} value=${user[key]}`);
}

Alright this brings us to the end of another tutorial. See you in the next one. Have fun coding.

← Prev

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT