Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using hasOwnProperty() to Check Object Properties | Advanced Object Manipulation Techniques
JavaScript Data Structures

bookUsing 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:

12345678910
const baseExercise = { category: "Strength", }; const exercise = Object.create(baseExercise); exercise.name = "Deadlift"; for (let key in exercise) { console.log(key); }
copy

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.

12345678910111213
const 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]}`); } }
copy

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?

question mark

What is the primary purpose of the hasOwnProperty() method?

Select the correct answer

question mark

What type of value does the hasOwnProperty() method return?

Select the correct answer

question mark

When using hasOwnProperty() with the for...in loop, why is it important to check for own properties?

Select the correct answer

question mark

In the code below, what will be logged when iterating through the video object's properties?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookUsing 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:

12345678910
const baseExercise = { category: "Strength", }; const exercise = Object.create(baseExercise); exercise.name = "Deadlift"; for (let key in exercise) { console.log(key); }
copy

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.

12345678910111213
const 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]}`); } }
copy

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?

question mark

What is the primary purpose of the hasOwnProperty() method?

Select the correct answer

question mark

What type of value does the hasOwnProperty() method return?

Select the correct answer

question mark

When using hasOwnProperty() with the for...in loop, why is it important to check for own properties?

Select the correct answer

question mark

In the code below, what will be logged when iterating through the video object's properties?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt