An object is a collection of properties, where each property is a key-value pair.
An object can be created using curly braces {} with key-value pairs inside.
let person = {
name: "Alice",
age: 30,
isStudent: false
};let person = {
name: "Alice",
age: 30,
isStudent: false
};let person = {
name: "Alice",
age: 30,
isStudent: false
};Properties can be accessed using dot notation or bracket notation.
console.log(person.name);
console.log(person["age"]);
console.log(person.name);
console.log(person["age"]);
console.log(person.name);
console.log(person["age"]);
Properties can be changed or added dynamically.
person.age = 31;
person.city = "New York";
console.log(person);
person.age = 31;
person.city = "New York";
console.log(person);
person.age = 31;
person.city = "New York";
console.log(person);
The delete keyword removes a property from an object.
delete person.isStudent;
console.log(person);
delete person.isStudent;
console.log(person);
delete person.isStudent;
console.log(person);
The in operator checks if a property exists.
console.log("age" in person);
console.log("salary" in person); console.log("age" in person);
console.log("salary" in person); console.log("age" in person);
console.log("salary" in person); Objects can be looped through using for...in.
for (let key in person) {
console.log(key + ": " + person[key]);
}for (let key in person) {
console.log(key + ": " + person[key]);
}for (let key in person) {
console.log(key + ": " + person[key]);
}push(value) adds an element to the end.
pop() removes the last element.
unshift(value) adds an element to the beginning.
shift() removes the first element.
fruits.push("Mango");
fruits.pop();
fruits.unshift("Strawberry");
fruits.shift();
console.log(fruits);fruits.push("Mango");
fruits.pop();
fruits.unshift("Strawberry");
fruits.shift();
console.log(fruits);fruits.push("Mango");
fruits.pop();
fruits.unshift("Strawberry");
fruits.shift();
console.log(fruits);Objects can contain arrays, and arrays can contain objects.
let student = {
name: "Emily",
grades: [90, 85, 88]
};
console.log(student.grades[1]); let student = {
name: "Emily",
grades: [90, 85, 88]
};
console.log(student.grades[1]); let student = {
name: "Emily",
grades: [90, 85, 88]
};
console.log(student.grades[1]); let employees = [
{ name: "Alice", position: "Developer" },
{ name: "Bob", position: "Designer" }
];
console.log(employees[0].name); let employees = [
{ name: "Alice", position: "Developer" },
{ name: "Bob", position: "Designer" }
];
console.log(employees[0].name); let employees = [
{ name: "Alice", position: "Developer" },
{ name: "Bob", position: "Designer" }
];
console.log(employees[0].name); Objects and arrays are crucial for managing and structuring data in JavaScript. The next section will focus on ES6+ features, which introduce new syntax and functionalities to improve JavaScript development.