DEV Community

Cover image for Objects in JS
Kavitha
Kavitha

Posted on

Objects in JS

1) What is an Object?

An object stores data in key : value pairs.

Syntax

let mobile = {
    brand: "Vivo",
    model: "V70",
    price: 25000
};
Enter fullscreen mode Exit fullscreen mode

Here,

  • brand → key
  • "Vivo" → value

Accessing Object Values

console.log(mobile.brand);
console.log(mobile.model);
Enter fullscreen mode Exit fullscreen mode

Output

Vivo
V70
Enter fullscreen mode Exit fullscreen mode

Real-World Example

let student = {
    name: "Kavitha",
    age: 20,
    department: "EEE"
};

console.log(student.name);
Enter fullscreen mode Exit fullscreen mode

Output:

Kavitha
Enter fullscreen mode Exit fullscreen mode

2) Creating Object Inside an Object

This is called a nested object.

It means one object is stored inside another object.

Example: Mobile Object

let mobile = {
    name: "Vivo",
    model: "V70",

    camera: {
        rear: "32MP",
        front: "64MP"
    }
};
Enter fullscreen mode Exit fullscreen mode

Here camera itself is another object.

Access Nested Object

console.log(mobile.camera.rear);
console.log(mobile.camera.front);
Enter fullscreen mode Exit fullscreen mode

Output

32MP
64MP
Enter fullscreen mode Exit fullscreen mode

Real-World Understanding

Think like this:

A student has an address

Address itself has:

  • city
  • state
  • pincode

So:

let student = {
    name: "Kavitha",

    address: {
        city: "Tirunelveli",
        state: "Tamil Nadu",
        pincode: 627001
    }
};
Enter fullscreen mode Exit fullscreen mode

Access:

console.log(student.address.city);
Enter fullscreen mode Exit fullscreen mode

Output:

Tirunelveli
Enter fullscreen mode Exit fullscreen mode

3) Creating Function Inside an Object

  • When a function is created inside an object, it is called a method.
  • Methods represent actions.

Example

let mobile = {
    name: "Vivo",

    call: function() {
        console.log("Calling...");
    }
};
Enter fullscreen mode Exit fullscreen mode

Calling the Function

mobile.call();
Enter fullscreen mode Exit fullscreen mode

Output:

Calling...
Enter fullscreen mode Exit fullscreen mode

Short Modern Syntax

This is the cleaner way:

let mobile = {
    name: "Vivo",

    call() {
        console.log("Calling...");
    }
};
Enter fullscreen mode Exit fullscreen mode
  • This is commonly used.

4) Object + Nested Object + Function Together

  • This is the complete structure
let mobile = {
    name: "Vivo",
    model: "V70",

    camera: {
        rear: "32MP",
        front: "64MP"
    },

    display: {
        size: "6.8 inch",
        type: "AMOLED"
    },

    calling() {
        console.log("Calling Amma...");
    },

    browsing() {
        console.log("Browsing...");
    }
};
Enter fullscreen mode Exit fullscreen mode

Access Everything

console.log(mobile.name);
console.log(mobile.camera.front);
console.log(mobile.display.size);

mobile.calling();
mobile.browsing();
Enter fullscreen mode Exit fullscreen mode

Output

Vivo
64MP
6.8 inch
Calling Amma...
Browsing...
Enter fullscreen mode Exit fullscreen mode

Why Do We Use This?

This is heavily used in:

  • JavaScript projects
  • React
  • APIs
  • JSON data
  • backend responses
  • real-world applications

Example:

user.profile.name
product.details.price
robot.sensor.temperature
Enter fullscreen mode Exit fullscreen mode

Simple Memory Trick

Object = thing

Nested object = thing inside thing

Function inside object = action of that thing

Example:

  • Car → object
  • Engine → object inside object
  • Start → function inside object

Top comments (0)