JavaScript Objects

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.

Creating Objects

There are multiple ways to create objects in JavaScript:

Object Literal
Try yourself
        
            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.


Using the new Object() Syntax
Try yourself
        
            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.


Using a Constructor Function
Try yourself
        
            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.


Accessing Object Properties

Object properties can be accessed using dot notation or bracket notation.

Dot Notation
Try yourself
        
            const person = {
    firstName: "John",
    lastName: "Doe"
};
console.log(person.firstName); // Outputs: John
        
    

This is the most common way to access object properties.


Bracket Notation
Try yourself
        
            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.


Adding and Deleting Properties

Properties can be added or deleted dynamically from an object.

Adding Properties
Try yourself
        
            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.


Deleting Properties
Try yourself
        
            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 in Objects

Methods are functions stored as object properties.

Defining Methods
Try yourself
        
             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

Comparing objects in JavaScript can be tricky because they are compared by reference, not by value.

Reference Comparison
Try yourself
        
            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.


Shallow Comparison
Try yourself
        
            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.


Important Notes

Here are some important notes and best practices when working with objects in JavaScript:


Summary

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.