JavaScript Objects

JavaScript Objects

JavaScript Objects

Objects in JavaScript are collections of key-value pairs. They allow us to group related data and functionality together.

1️⃣ Creating an Object

Using Object Literal {} (Most Common)

const person = { name: "John", age: 30, isStudent: false }; console.log(person.name); // Output: John console.log(person["age"]); // Output: 30

📌 You can access properties using dot notation (object.property) or bracket notation (object["property"]).

Using new Object() (Less Common)

const person = new Object(); person.name = "Alice"; person.age = 25; console.log(person.name); // Output: Alice

📌 The object literal {} is preferred because it is simpler and more readable.

2️⃣ Nested Objects

Objects can contain other objects inside them.

const user = { name: "Emily", address: { city: "New York", zip: "10001" } }; console.log(user.address.city); // Output: New York

3️⃣ Adding, Updating, and Deleting Properties

Add a New Property

const car = { brand: "Toyota" }; car.model = "Corolla"; console.log(car.model); // Output: Corolla

Update a Property

car.brand = "Honda"; console.log(car.brand); // Output: Honda

Delete a Property

delete car.model; console.log(car.model); // Output: undefined

4️⃣ Methods in Objects (Functions as Properties)

Objects can have methods (functions inside objects).

const person = { name: "David", greet: function() { return `Hello, my name is ${this.name}!`; } }; console.log(person.greet()); // Output: Hello, my name is David!

📌 this refers to the current object.

5️⃣ Object.keys(), Object.values(), Object.entries()

These methods help in working with object data.

const book = { title: "JavaScript Guide", author: "John Doe", year: 2022 }; console.log(Object.keys(book)); // Output: ["title", "author", "year"] console.log(Object.values(book)); // Output: ["JavaScript Guide", "John Doe", 2022] console.log(Object.entries(book)); // Output: [["title", "JavaScript Guide"], ["author", "John Doe"], ["year", 2022]]

6️⃣ Object Spread (...) and Object.assign()

These are used to copy objects.

Using Spread (...)

const obj1 = { a: 1, b: 2 }; const obj2 = { ...obj1, c: 3 }; console.log(obj2); // Output: { a: 1, b: 2, c: 3 }

Using Object.assign()

const obj3 = Object.assign({}, obj1, { d: 4 }); console.log(obj3); // Output: { a: 1, b: 2, d: 4 }

7️⃣ Checking If a Property Exists

Use in operator or hasOwnProperty().

const user = { name: "Mark", age: 40 }; console.log("name" in user); // Output: true console.log(user.hasOwnProperty("age"));// Output: true console.log("address" in user); // Output: false

8️⃣ Looping Over Object Properties

Use for...in loop.

const student = { name: "Liam", grade: "A" }; for (let key in student) { console.log(`${key}: ${student[key]}`); } // Output: // name: Liam // grade: A

9️⃣ Object Freezing and Sealing

Prevent object modifications.

Freeze (No Changes Allowed)

const user = { name: "Sophia" }; Object.freeze(user); user.name = "Emma"; // ❌ No change allowed console.log(user.name); // Output: Sophia

Seal (Only Updates Allowed, No New Properties)

const user2 = { name: "Jake" }; Object.seal(user2); user2.name = "Mike"; // ✅ Allowed user2.age = 25; // ❌ Not Allowed console.log(user2); // Output: { name: "Mike" }

🔹 Summary

Objects store data as key-value pairs
Access propertiesobject.property or object["property"]
Nested objectsobject.innerObject.property
Methods (functions inside objects)
Check property existence"key" in object or object.hasOwnProperty("key")
Loop through objectsfor...in
Copy objectsObject.assign() or { ...object }
Freeze (Object.freeze()) and Seal (Object.seal())

🚀 Now you're an object pro! Let me know if you have questions. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close