Using hasOwnProperty() to Check Object Properties
When iterating through an object's properties using the for...in loop, it's important to distinguish between the object's own properties and properties inherited from its prototype.
In JavaScript, some properties may appear during iteration even if they were not defined directly on the object. This happens because objects can inherit properties from other objects.
The hasOwnProperty() method helps us ensure that we work only with the properties that belong directly to the object.
Understanding hasOwnProperty()
The hasOwnProperty() method is a built-in JavaScript method that checks whether a property exists directly on an object. It returns a Boolean value (true or false).
Syntax
object.hasOwnProperty(property)
object: the object you want to check;property: the property name to look for.
Why hasOwnProperty() Is Useful
Consider the following example:
12345678910const baseExercise = { category: "Strength", }; const exercise = Object.create(baseExercise); exercise.name = "Deadlift"; for (let key in exercise) { console.log(key); }
Even though category was not defined directly on exercise, it appears in the loop. This is why we need an additional check.
Using hasOwnProperty() with for...in
By combining for...in with hasOwnProperty(), you can safely iterate only over the object's own properties.
12345678910111213const exercise = { name: "Deadlift", primaryMusclesWorked: "Erector spinae", equipment: "Barbell", exerciseType: "Strength", difficulty: "Intermediate", }; for (let key in exercise) { if (exercise.hasOwnProperty(key)) { console.log(`Property: ${key}, Value: ${exercise[key]}`); } }
This check ensures that inherited properties are ignored, making your code more predictable and safer.
1. What is the primary purpose of the hasOwnProperty() method?
2. What type of value does the hasOwnProperty() method return?
3. When using hasOwnProperty() with the for...in loop, why is it important to check for own properties?
4. In the code below, what will be logged when iterating through the video object's properties?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.27
Using hasOwnProperty() to Check Object Properties
Swipe to show menu
When iterating through an object's properties using the for...in loop, it's important to distinguish between the object's own properties and properties inherited from its prototype.
In JavaScript, some properties may appear during iteration even if they were not defined directly on the object. This happens because objects can inherit properties from other objects.
The hasOwnProperty() method helps us ensure that we work only with the properties that belong directly to the object.
Understanding hasOwnProperty()
The hasOwnProperty() method is a built-in JavaScript method that checks whether a property exists directly on an object. It returns a Boolean value (true or false).
Syntax
object.hasOwnProperty(property)
object: the object you want to check;property: the property name to look for.
Why hasOwnProperty() Is Useful
Consider the following example:
12345678910const baseExercise = { category: "Strength", }; const exercise = Object.create(baseExercise); exercise.name = "Deadlift"; for (let key in exercise) { console.log(key); }
Even though category was not defined directly on exercise, it appears in the loop. This is why we need an additional check.
Using hasOwnProperty() with for...in
By combining for...in with hasOwnProperty(), you can safely iterate only over the object's own properties.
12345678910111213const exercise = { name: "Deadlift", primaryMusclesWorked: "Erector spinae", equipment: "Barbell", exerciseType: "Strength", difficulty: "Intermediate", }; for (let key in exercise) { if (exercise.hasOwnProperty(key)) { console.log(`Property: ${key}, Value: ${exercise[key]}`); } }
This check ensures that inherited properties are ignored, making your code more predictable and safer.
1. What is the primary purpose of the hasOwnProperty() method?
2. What type of value does the hasOwnProperty() method return?
3. When using hasOwnProperty() with the for...in loop, why is it important to check for own properties?
4. In the code below, what will be logged when iterating through the video object's properties?
Thanks for your feedback!