0% found this document useful (0 votes)
34 views13 pages

JavaScript Object Literal Guide

The document provides an overview of JavaScript object literal syntax, including how to create, modify, and delete properties. It covers nested objects, object destructuring, dynamic property names, and methods within objects. Additionally, it discusses iterating over properties and checking for the existence of properties in an object.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views13 pages

JavaScript Object Literal Guide

The document provides an overview of JavaScript object literal syntax, including how to create, modify, and delete properties. It covers nested objects, object destructuring, dynamic property names, and methods within objects. Additionally, it discusses iterating over properties and checking for the existence of properties in an object.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

JavaScript

Training
Object Literal Syntax
const person = { const person = new Object();
name: "Alice", [Link] = "Alice";
age: 30, [Link] = 30;
isStudent: false
};

Key: name
Value: "Alice"
Object Literal Syntax
function Person(name, age) [Link]([Link]); /
{ / "Alice"
[Link] = name;
[Link] = age; [Link](person["age"]);
} // 30
const person = new
Person("Alice", 30);
Modifying and Adding Properties
[Link] = 31; // Modify
[Link] = "USA"; // Add new property
Deleting Properties
delete [Link];
Iterating Over Properties
for (let key in person) {
[Link](key, person[key]);
}

[Link](person); // ["name", "age", "country"]


[Link](person); // ["Alice", 31, "USA"]
[Link](person);
Nested Objects
const user = {
name: "John",
address: {
city: "New York",
zip: "10001"
}
};
[Link]([Link]); // "New York"
Object Destructuring
const person = { const { name: fullName } =
name: "Alice", person;
age: 30, [Link](fullName); //
country: "USA" "Alice"
};

const { name, age } = person;


[Link](name); // "Alice"
[Link](age); // 30
Object Methods
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const merged = [Link]({}, obj1, obj2); // { a: 1, b:
2}
Dynamic Property Names
(Computed Properties)
const key = "score";
const obj = {
[key]: 100
};
[Link]([Link]); // 100
Nested Objects with Methods

const user = {
name: "Jane",
contact: {
email: "jane@[Link]",
phone: "123-456-7890"
},
greet() {
[Link](`Hello, my name is ${[Link]}`);
}
};

[Link](); // Hello, my name is Jane


[Link]([Link]); // jane@[Link]
Object in Array
const users = [
{ id: 1, name: "Alice", age: 25 },
{ id: 2, name: "Bob", age: 30 }
];

// Access a property
[Link](users[0].name); // Alice

// Loop through array of objects


for (const user of users) {
[Link](`${[Link]} is ${[Link]} years old.`);
}
Checking for Properties
[Link]("name" in person); // true

[Link]("name"); // true

You might also like