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