Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Manipulating Object Properties | Understanding and Working with Objects
Objects and Prototypes in JavaScript

bookManipulating Object Properties

When working with JavaScript objects, you often need to add, update, or remove properties to keep your data accurate and up-to-date. Understanding how to manipulate object properties is essential for dynamic applications, where the structure of your data can change during execution. You can add new properties to an object simply by assigning a value to a new property name, update existing properties by assigning a new value to an existing property, and remove properties with the delete operator.

12345678910111213
// Adding and updating properties const user = { name: "Alice", age: 28 }; // Add a new property user.email = "alice@example.com"; // Update an existing property user.age = 29; console.log(JSON.stringify(user));
copy
1234567891011
// Deleting properties const product = { id: 101, name: "Book", price: 12.99 }; // Remove the price property delete product.price; console.log(JSON.stringify(product));
copy

After manipulating properties, you may want to check whether a property exists on an object. JavaScript provides a couple of ways to do this: the in operator and the hasOwnProperty method.

  • The in operator checks if a property exists anywhere in the object's prototype chain;
  • The hasOwnProperty checks only for properties directly on the object itself. For example, after adding and deleting properties as shown above, you can use these checks to confirm the presence or absence of specific properties.
123456789101112131415
const car = { make: "Toyota", model: "Corolla" }; // Add a new property car.year = 2018; // Check property existence with 'in' console.log("year" in car); // true console.log("color" in car); // false // Check property existence with hasOwnProperty console.log(car.hasOwnProperty("model")); // true console.log(car.hasOwnProperty("toString")); // false (inherited from prototype)
copy
question mark

You have the following code manipulating an object. Consider the state of the data object after all operations.

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain the difference between the `in` operator and `hasOwnProperty` in more detail?

How do I remove a property from an object if I don't know its name in advance?

Are there any best practices for updating or deleting properties in JavaScript objects?

Awesome!

Completion rate improved to 7.69

bookManipulating Object Properties

Deslize para mostrar o menu

When working with JavaScript objects, you often need to add, update, or remove properties to keep your data accurate and up-to-date. Understanding how to manipulate object properties is essential for dynamic applications, where the structure of your data can change during execution. You can add new properties to an object simply by assigning a value to a new property name, update existing properties by assigning a new value to an existing property, and remove properties with the delete operator.

12345678910111213
// Adding and updating properties const user = { name: "Alice", age: 28 }; // Add a new property user.email = "alice@example.com"; // Update an existing property user.age = 29; console.log(JSON.stringify(user));
copy
1234567891011
// Deleting properties const product = { id: 101, name: "Book", price: 12.99 }; // Remove the price property delete product.price; console.log(JSON.stringify(product));
copy

After manipulating properties, you may want to check whether a property exists on an object. JavaScript provides a couple of ways to do this: the in operator and the hasOwnProperty method.

  • The in operator checks if a property exists anywhere in the object's prototype chain;
  • The hasOwnProperty checks only for properties directly on the object itself. For example, after adding and deleting properties as shown above, you can use these checks to confirm the presence or absence of specific properties.
123456789101112131415
const car = { make: "Toyota", model: "Corolla" }; // Add a new property car.year = 2018; // Check property existence with 'in' console.log("year" in car); // true console.log("color" in car); // false // Check property existence with hasOwnProperty console.log(car.hasOwnProperty("model")); // true console.log(car.hasOwnProperty("toString")); // false (inherited from prototype)
copy
question mark

You have the following code manipulating an object. Consider the state of the data object after all operations.

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt