android-logo

JavaScript Object


A JavaScript object is a non-primitive data type that allows you to store various data collections.

Declaring object

Syntax


const object_name = {
    key1: value1,
    key2: value2
 }


Lets take an example of JavaScript object


const employee = {
    employeeId : 1701
    firstName: 'Vishal',
    lastName: 'Torgal'
};

Here employee is an object which stores values such as numbers and strings


Accessing object by dot notation


const employee = { 
    employeeId: 603, 
    employeeName: 'Vishal' 
};

// accessing property 
console.log(employee.employeeName); 

// output
Vishal


Accessing object by bracket notation


const employee = { 
    employeeId: 603, 
    employeeName: 'Vishal' 
};

// accessing property 
console.log(employee["employeeId"]);

// output
603


Accessing nested object



const employee = { 
    employeeId: 603, 
    employeeName: 'Vishal', 
    title: {
        role: 'developer'
    }
}

// accessing property
console.log(employee.title.role); 

// output
developer


previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.