Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 7.69

bookManipulating Object Properties

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
some-alt