Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Objects

Introduction

JavaScript object is a non-primitive data-type that means it allows you to store multiple collections of data.

When to Use Objects

Objects are used to represent a “thing” in your code. That could be a name, a person, a car, a book, a table — basically anything that is made up or can be defined by a set of characteristics. In objects, these characteristics are called properties

Properties consists of a Key and a Value .

Syntax

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

Example

const identity = { name: 'Ram', age: 19 };

Here,

name and age are keys and, ram and 19 are Values. Combinely Termed as Properties.

Parameter Representation

Accessing Object Values

For This, You Have two ways to access the Values

  1. Dot (.) Representations
objectName.key

Bracket( [ ] ) Representation

objectName["keyName"]

Example:

const identity = { name: 'Ram', age: 19 }; console.log(identity.name); console.log(identity["name"); // Output // Ram // Ram

Note : Both will Result Same.

Assigning Value to Properties

You can add new properties to an existing object by simply giving it a value.

identity.name = "John";

Deleting Properties

The delete keyword deletes a property from an object:

Syntax

delete objectName.propertyName;

Example :

const identity = { name: 'Ram', age: 19 , hello : function() { console.log("Hello World"); } }; delete identity.age; // or you can use delete identity["age"];

JavaScript for...in Loop

The JavaScript for...in statement loops through the properties of an object to Print and Perform Action on Each Object Property.

Syntax :

for (variable in object) { // statements }

The block of code inside of the for...in loop will be executed once for each property.

Example 2:

const monthlySalary = { Ram : 5000, Shyam : 8000, Shivam : 9000, Sahil : 5000, Jatin : 10000 }; for (x in monthlySalary) { console.log(monthlySalary[x]); } // Output // 5000 // 8000 // 9000 // 5000 // 10000

Here,

First, We have declared the Object monthlySalary and then assigned salaries of the person.

for...in loop will Iterate through all the Elements of the Object. i.e. it will print all the salaries of Persons.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?