In JavaScript, objects are used to store collections of data and more complex entities. Objects are created using curly braces {} and can contain properties and methods.
There are multiple ways to create objects in JavaScript:
Object Literal
let person = {
firstName: "John",
lastName: "Doe",
age: 30
};
This is the simplest way to create an object. Properties and values are defined inside curly braces.
const person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 30;
person.greet = function() {
console.log("Hello, " + this.firstName);
};
console.log(person.firstName); // Outputs: John
This method uses the Object constructor to create an object.
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
var person = new Person("John", "Doe", 30);
Constructor functions allow you to create multiple instances of objects with the same properties and methods.
Object properties can be accessed using dot notation or bracket notation.
Dot Notation
const person = {
firstName: "John",
lastName: "Doe"
};
console.log(person.firstName); // Outputs: John
This is the most common way to access object properties.
const person = {
"first-name": "John",
"last-name": "Doe"
};
console.log(person["first-name"]); // Outputs: John
This method allows you to access properties using variables or special characters.
Properties can be added or deleted dynamically from an object.
Adding Properties
const person = {
firstName: "John",
lastName: "Doe"
};
person.age = 30;
console.log(person.age); // Outputs: 30
Use dot notation or bracket notation to add new properties.
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
delete person.age;
console.log(person.age); // Outputs: undefined
Use the delete operator to remove properties from an object.
Methods are functions stored as object properties.
Defining Methods
const person = {
firstName: "John",
lastName: "Doe",
greet: function() {
console.log("Hello, " + this.firstName);
}
};
person.greet(); // Outputs: Hello, John
Methods are defined similarly to properties, but their values are functions.
Comparing objects in JavaScript can be tricky because they are compared by reference, not by value.
Reference Comparison
const person1 = { firstName: "John" };
const person2 = { firstName: "John" };
console.log(person1 === person2); // Outputs: false
This example demonstrates how two objects with the same properties and values are not considered equal because they do not reference the same object.
function shallowEqual(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (let key of keys1) {
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
const person1 = { firstName: "John", lastName: "Doe" };
const person2 = { firstName: "John", lastName: "Doe" };
console.log(shallowEqual(person1, person2)); // Outputs: true
A shallow comparison checks if the properties and values of two objects are the same.
Here are some important notes and best practices when working with objects in JavaScript:
JavaScript objects are essential for storing and managing data. By understanding how to create, modify, and compare objects, you can write more efficient and maintainable code.