JavaScript / JavaScript Arrays and Objects

Manipulating Object Properties

This tutorial focuses on manipulating properties of JavaScript objects. We'll explore how to add, update, and delete properties, and the key concepts involved.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Explains how to work with arrays and objects to store and manipulate data effectively.

1. Introduction

In this tutorial, we aim to provide a concise overview of manipulating properties of JavaScript objects. We'll focus on how to add, update, and delete properties, exploring the key concepts involved.

By the end of this tutorial, you will be able to:

  • Understand basic principles of JavaScript objects
  • Manipulate properties of JavaScript objects (add, update, and delete)

Before we begin, ensure you are familiar with JavaScript basics, particularly variables, data types, and functions.

2. Step-by-Step Guide

JavaScript objects are mutable, meaning they can be changed after creation. Properties can be added, updated, and deleted.

Adding Properties: To add a new property to an object, simply assign a value to a new property name.

let obj = {}; // an empty object
obj.newProp = "Hello"; // adds a new property 'newProp' with value 'Hello'

Updating Properties: Updating a property is similar to adding - simply assign a new value to an existing property.

obj.newProp = "Hi"; // updates 'newProp' value to 'Hi'

Deleting Properties: To delete a property, we use the delete operator.

delete obj.newProp; // removes 'newProp' from object

Remember, trying to access a property that doesn't exist will return undefined.

3. Code Examples

Let's manipulate some properties!

// Create an object
let person = {
  name: "John Doe",
  age: 30
};

// Add a new property
person.job = "Developer";
console.log(person.job); // Expected output: "Developer"

// Update a property
person.name = "Jane Doe";
console.log(person.name); // Expected output: "Jane Doe"

// Delete a property
delete person.age;
console.log(person.age); // Expected output: undefined

4. Summary

We've covered the basics of JavaScript object property manipulation. We've learned how to add, update, and delete properties. Remember to use the dot notation for property manipulation, and that accessing a non-existent property returns undefined.

Continue learning by exploring built-in object methods, like Object.keys(), Object.values(), and Object.entries().

5. Practice Exercises

Exercise 1: Create a car object with properties: brand, model, and year. Then, add a color property.

Exercise 2: Update the year property of the car object to the current year.

Exercise 3: Delete the model property from the car object.

Solutions:

// Exercise 1
let car = {
  brand: "Tesla",
  model: "Model S",
  year: 2018
};
car.color = "Red";

// Exercise 2
car.year = new Date().getFullYear();

// Exercise 3
delete car.model;

Keep practicing by creating and manipulating your own objects. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help