While the for...in loop in JavaScript offers a convenient way to iterate over an object’s properties, it’s crucial to remember that it encompasses properties inherited from the prototype chain. This means you’ll also encounter properties defined in parent objects or prototypes. Here’s an example:
To specifically target properties directly owned by the Animal instance, we leverage the hasOwnProperty() method. This method determines whether a property is directly defined on the object itself, as opposed to being inherited through the prototype chain.