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.
Loading...
Related Quiz
- You want to select an element with the ID 'special' using JavaScript. However, your code isn't working as expected. What could be the possible reason if the HTML structure is correct?
- How to declare a block-scoped variable in JavaScript?
- A function declaration is hoisted to the top of the ________ in which it was defined.
- Which part of a "for" loop is executed only once, when the loop starts?
- In order to make an object iterable with a for...of loop, you need to define its _______ method.