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

Conteúdo do Curso

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Fundamentals of JavaScript Objects
3. Advanced Object Manipulation Techniques
4. Mastering JavaScript Arrays
5. Advanced Array Methods and Transformations

book
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 those inherited from its prototype chain.

The hasOwnProperty() method is a valuable tool for checking whether a property belongs to the object or is inherited from a prototype. Let's explore the hasOwnProperty() method and how it can safely handle object properties during iteration.

Understanding hasOwnProperty()

The hasOwnProperty() method is a built-in JavaScript method that allows us to check if a specific property exists directly on an object. It returns a Boolean value (true or false) indicating whether the object has a property with the specified name.

Here is the basic syntax of the hasOwnProperty() method:

  • object: The object you want to check;
  • property: The property name we want to check for.

Using hasOwnProperty() with for...in

Using the for...in loop, we can combine it with the hasOwnProperty() method to iterate through an object's properties. It's important to add a check for own properties at each iteration to ensure that we only access the object's own properties. Even if we are certain that the object has no inherited properties, this check will protect against future possible errors.

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

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?

Selecione a resposta correta

question mark

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

Selecione a resposta correta

question mark

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

Selecione a resposta correta

question mark

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

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt