Objects in JavaScript are key-value pairs that allow you to store and manage complex data structures. They provide an efficient way to group related data and define custom behavior for that data using methods. Objects can contain properties (data) and methods (functions) that act upon the data.
{}
and a comma-separated list of key-value pairs.javascriptlet person = {
firstName: "John",
lastName: "Doe",
age: 30,
city: "New York",
};
Object
constructor.javascriptlet person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 30;
person.city = "New York";
javascriptfunction Person(firstName, lastName, age, city) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.city = city;
}
let person1 = new Person("John", "Doe", 30, "New York");
let person2 = new Person("Jane", "Smith", 28, "Los Angeles");
javascriptconsole.log(person.firstName); // "John"
console.log(person.age); // 30
javascriptconsole.log(person["lastName"]); // "Doe"
console.log(person["city"]); // "New York"
javascriptperson.age = 31;
console.log(person.age); // 31
person["city"] = "San Francisco";
console.log(person["city"]); // "San Francisco"
javascriptperson.job = "Software Developer";
console.log(person.job); // "Software Developer"
delete
keyword.javascriptdelete person.job;
console.log(person.job); // undefined
javascriptlet person = {
firstName: "John",
lastName: "Doe",
age: 30,
city: "New York",
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
console.log(person.fullName()); // "John Doe"
javascriptlet descriptor = Object.getOwnPropertyDescriptor(person, "firstName");
console.log(descriptor);
/*
{
value: "John",
writable: true,
enumerable: true,
configurable: true
}
*/
Object.defineProperty()
.javascriptObject.defineProperty(person, "firstName", {
enumerable: false,
configurable: false,
});
for...in
loop, which iterates over the enumerable properties of an object.javascriptfor (let key in person) {
console.log(key + ": " + person[key]);
}
/*
firstName: John
lastName: Doe
age: 30
city: New York
fullName: function() {...}
*/
javascriptlet keys = Object.keys(person);
console.log(keys); // ["firstName", "lastName", "age", "city", "fullName"]
javascriptlet values = Object.values(person);
console.log(values); // ["John", "Doe", 30, "New York", function() {...}]
javascriptlet entries = Object.entries(person);
console.log(entries);
/*
[
["firstName", "John"],
["lastName", "Doe"],
["age", 30],
["city", "New York"],
["fullName", function() {...}]
]
javascriptlet personCopy = Object.assign({}, person);
console.log(personCopy); // {firstName: "John", lastName: "Doe", age: 30, city: "New York", fullName: function() {...}}
Object.freeze()
method is used to prevent any modification to an object (i.e., making it immutable). The Object.isFrozen()
method returns a boolean indicating whether an object is frozen.javascriptObject.freeze(person);
console.log(Object.isFrozen(person)); // true
Objects are a fundamental aspect of JavaScript and provide a powerful way to manage and manipulate complex data structures. By understanding how to create, access, modify, and manage objects, you can build more efficient and flexible applications.