DEV Community

Cover image for Objects in JS
Rakshambika
Rakshambika

Posted on

Objects in JS

What is an Object?

  • An Object is a non-primitive data type used to store data in key-value pairs.
  • It is a dynamic data structure, meaning properties can be added, updated, or removed at runtime.
  • Objects are mutable, which means their contents can be changed after creation.

Why do we use Objects?

  • Objects are used to store related data and behavior in a single variable. Instead of creating multiple separate variables, we can group them together using an object.

Without Object:

// Without Object

let name="Raksha";
let age=21;
let location="Chennai";

console.log(name);
console.log(age);
console.log(location);

Enter fullscreen mode Exit fullscreen mode

Output:

Raksha
21
Chennai
Enter fullscreen mode Exit fullscreen mode

With Object:

// With Objects

let student = {
    name: "Raksha",
    age: 22,
    city: "Chennai"
};

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

Output:

{ name: 'Raksha', age: 22, city: 'Chennai' }
Enter fullscreen mode Exit fullscreen mode

Advantages of objects:

  • Used to organize the related data in a single variable.
  • Easy to access and update data.
  • Can store different types of value.
  • Can store functions.

Creating Objects in JavaScript:

Example:

let student = {
    name: "Raksha",
    age: 22,
    city: "Chennai"
};

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

Output:

{ name: 'Raksha', age: 22, city: 'Chennai' }
Enter fullscreen mode Exit fullscreen mode

Accessing Objects in JavaScript:

Once an object is created, we can access its properties and methods using two main ways:

1. Dot notation (.)
2. Bracket notation ([])

1. Dot Notation:

We use the dot (.) followed by the property name.

syntax:

object.property
Enter fullscreen mode Exit fullscreen mode

Example:

// Dot Notation

let home={
    NoofMembers:4,
    type:"Independent House",
    noofrooms:4,
    location:"Thiruvarur"
}

console.log(home.NoofMembers);
console.log(home.location);
Enter fullscreen mode Exit fullscreen mode

2. Bracket Notation:

We use square brackets [] and specify the property name as a string or symbol.

Syntax:

object["property"]
Enter fullscreen mode Exit fullscreen mode

Example:

// Bracket Notation

let house={
    NoofMembers:4,
    type:"Independent House",
    noofrooms:4,
    location:"Thiruvarur"
}

console.log(house["noofrooms"]);
console.log(house["type"]);
Enter fullscreen mode Exit fullscreen mode

Why do we need bracket notation?

It is especially useful when the property name is stored in a variable.

Example:

let student = {
    name: "Raksha",
    age: 22
};

let key = "name";

console.log(student[key]);
Enter fullscreen mode Exit fullscreen mode

Output:

Raksha
Enter fullscreen mode Exit fullscreen mode

Adding Properties:

let house={
    NoofMembers:4,
    type:"Independent House",
    noofrooms:4,
    location:"Thiruvarur"
}

house.colour="Blue";
house.floors=2;

console.log(house);
Enter fullscreen mode Exit fullscreen mode

Output:

{
  NoofMembers: 4,
  type: 'Independent House',
  noofrooms: 4,
  location: 'Thiruvarur',
  colour: 'Blue',
  floors: 2
}
Enter fullscreen mode Exit fullscreen mode

Modifying Properties:

let classroom={
   teacher:"Kavitha",
   noofStudents:50
}
console.log(classroom);
classroom.noofStudents=70;
console.log(classroom);
Enter fullscreen mode Exit fullscreen mode

Output:

Before Modification:
{ teacher: 'Kavitha', noofStudents: 50 }

After Modification:
{ teacher: 'Kavitha', noofStudents: 70 }
Enter fullscreen mode Exit fullscreen mode

Deleting Properties:

// Delete

let animal={
    name:"Tiger",
    colour:"Orange",
    gender:"Male",
    isActive:true,
    noofsiblings:5
}

console.log(animal);
delete animal.isActive;
console.log(animal);
Enter fullscreen mode Exit fullscreen mode

Output:

Before Deletion:
{
  name: 'Tiger',
  colour: 'Orange',
  gender: 'Male',
  isActive: true,
  noofsiblings: 5
}

After Deletion:
{
  name: 'Tiger',
  colour: 'Orange',
  gender: 'Male',
  noofsiblings: 5
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

We use square brackets [] and specify the property name as a string.

Or Symbol.

Collapse
 
raksha_murugesan profile image
Rakshambika

Thank you for pointing that out! 😊 You're right. Object property keys can be strings or Symbols. I'll update the explanation to make it more technically complete.