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

Contenido del Curso

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

book
Method hasOwnProperty()

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?
What is the primary purpose of the `hasOwnProperty()` method?

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

Selecciona la respuesta correcta

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

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

Selecciona la respuesta correcta

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

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

Selecciona la respuesta correcta

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

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt