Skip to content Skip to footer

How to loop through or enumerate the properties of a JavaScript object?

There are multiple ways to iterate/loop through all the properties in a JavaScript object.

Method 1: for-in loop


var person = {
  name: 'Shravan Kasagoni',
  age: 20,
  isActive: true
};

person.constructor.prototype.printName = function() {
  console.log(this.name);
};

person.constructor.prototype.encKey = 'somevalue';

person.constructor.prototype.printName = function() {
  console.log(this.name);
};

The for-in loop iterates over all enumerable properties of an object.


for (var propertyName in person) {
  console.log(propertyName, person[propertyName]);
}

Result:


name Shravan Kasagoni
age 20
isActive true
printName ƒ () {
  console.log(this.name);
}
encKey somevalue