How to iterate over an object properties using JavaScript

   JavaScript

The for…in statement iterates over all the properties of an object that are strings.

Syntax

for (var in object){
  // statement goes here
}

Solution

const obj = { name: 'Nitesh', place: 'Bangalore', country: 'India' };

for (const prop in obj) {
  console.log(prop +': ' + obj[prop]);
}

// expected output:
// "name: Nitesh"
// "place: Bangalore"
// "country: India"
Tags: IterateJavaScriptObject

Leave a Comment