You need to filter out the prototype properties while using a loop to iterate over object properties. Which loop would you use, and what additional method would be needed to avoid iterating over prototype properties?

  • for...in loop with hasOwnProperty()
  • for...of loop with Object.keys()
  • while loop with Object.getOwnPropertyNames()
  • forEach() method with Object.getOwnPropertyNames()
You would use a for...of loop with Object.keys() to iterate over object properties while excluding prototype properties. Object.keys() returns an array of an object's own enumerable property names, ensuring that prototype properties are filtered out.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *