Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

var obj = { 

name: "Simon", 

age: "20", 

clothing: { 

style: "simple",

hipster: false 

  } 

for(var propt in obj){ 

console.log(propt + ': ' + obj[propt]);

}

How does the variable propt represent the properties of the object? It's not a built-in method or property. Why does it come up with every property in the object?

1 Answer

0 votes
by (106k points)
If you want to iterate though object properties than you can use hasOwnProperty method see the code below:-

for (var property in object) { 

if (object.hasOwnProperty(property)) { 

// do stuff 

 } 

}

The hasOwnProperty method simply checks to see if this is a property specific to this class, and not one inherited from the base class.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 12, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...