An object is a data structure in JavaScript that stores values in key–value pairs. Objects are used to describe entities with multiple characteristics (e.g., a user with a name, age, access rights, etc.).
An object in JavaScript is a collection of properties. Each property is a key (name) and a value.
const user = {
name: "Anya",
age: 28,
isAdmin: true
};
const car = {
brand: "Toyota",
year: 2022
};
new Object()
const car = new Object();
car.brand = "Toyota";
car.year = 2022;
console.log(user.name); // "Anya"
const key = "age";
console.log(user[key]); // 28
user.age = 30;
delete user.isAdmin;
const company = {
name: "TechCorp",
address: {
city: "Moscow",
street: "Lenina, 5"
}
};
for (let key in user) {
console.log(key, user[key]);
}
console.log("name" in user); // true
Objects are a fundamental part of JavaScript.
They allow you to store and organize data in a flexible and convenient way.
Understanding them is key to working with most structures in JavaScript.