,

JavaScript Object Manipulation: A Comprehensive Guide

Learn how to work with objects in JavaScript on Okpediaa! Object manipulation is a key part of JavaScript programming. In this article, we’ll show you how to create, modify, and use objects effectively.
Read on to improve your JavaScript skills, and explore more articles on Okpediaa!”

Object Manipulation in JavaScript

Creating Objects

Copied!
const person = { firstName: "John", lastName: "Doe", age: 30, };


Accessing Object Properties

Copied!
console.log(person.firstName); // "John" console.log(person["lastName"]); // "Doe"


Modifying Object Properties

Copied!
person.age = 31; person ["lastName"] = "Smith";


Adding New Properties

Copied!
person.email = "john.doe@example.com";


Deleting Properties

Copied!
delete person.age;


Checking if a Property Exists

Copied!
if ("email" in person) { console.log("Email property exists."); }


Object Methods

Copied!
const car = { make: "Toyota", model: "Camry", start: function () { console.log("Engine started."); }, stop() { console.log("Engine stopped."); }, car.start(); car.stop(); };

Looping Through Object Properties

Copied!
for (const key in person) { console.log('${key): ${person [key]}'); } // Using Object.keys const keys Object.keys (person); for (const key of keys) { console.log('${key): ${person [key]}`); }


Object Destructuring

Copied!
const { firstName, lastName = person; console.log(firstName); // "John" console.log(lastName); // "Smith"


Object Spread (ES6)

Copied!
const contactInfo = { email: "john.doe@example.com", phone: "555-555-5555", }; const updatedPerson = { person, contactInfo };

Leave a Reply

Your email address will not be published. Required fields are marked *