Objects in JavaScript

javascript dev.to

Objects in JavaScript

An object in JavaScript is used to store multiple related values in a single variable. Objects store data in the form of key-value pairs.

For example, a student has related information such as name, age, and course. Instead of creating separate variables, we can store all this information inside one object.

let student = {
    name: "John",
    age: 20,
    course: "JavaScript"
};

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

Output:

{
    name: "John",
    age: 20,
    course: "JavaScript"
}
Enter fullscreen mode Exit fullscreen mode

Here, student is an object. name, age, and course are called keys, and "John", 20, and "JavaScript" are called values.

Creating an Object

An object is created using curly braces {}. Each property consists of a key and a value separated by a colon :.

let product = {
    name: "Laptop",
    price: 50000,
    inStock: true
};
Enter fullscreen mode Exit fullscreen mode

In this object:

  • name, price, and inStock are keys.
  • "Laptop", 50000, and true are values.

Each key-value pair is separated by a comma.

Accessing Object Properties

We can access object properties in two ways:

  • Dot Notation
  • Bracket Notation

1. Dot Notation

Dot notation is the most common way to access an object property.

let product = {
    name: "Laptop",
    price: 50000
};

console.log(product.name);
console.log(product.price);
Enter fullscreen mode Exit fullscreen mode

Output:

Laptop
50000
Enter fullscreen mode Exit fullscreen mode

Here, product.name accesses the value of the name property.

2. Bracket Notation

Bracket notation uses square brackets [] to access object properties.

let product = {
    name: "Laptop",
    price: 50000
};

console.log(product["name"]);
console.log(product["price"]);
Enter fullscreen mode Exit fullscreen mode

Output:

Laptop
50000
Enter fullscreen mode Exit fullscreen mode

The property name is written as a String inside the square brackets.

Dot Notation vs Bracket Notation

Dot Notation Bracket Notation
Uses a dot . Uses square brackets []
Example: product.name Example: product["name"]
Simple and commonly used Useful when the property name is dynamic
Cannot directly use a variable as a property name Can use a variable as a property name

For example, bracket notation is useful when the property name is stored in a variable.

let product = {
    name: "Laptop",
    price: 50000
};

let key = "name";

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

Output:

Laptop
Enter fullscreen mode Exit fullscreen mode

Here, the value of key is "name", so product[key] accesses the name property.

Adding a New Property

We can add a new property to an existing object.

let user = {
    name: "John"
};

user.age = 25;

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

Output:

{
    name: "John",
    age: 25
}
Enter fullscreen mode Exit fullscreen mode

Here, the age property is added to the user object.

Updating an Object Property

We can update the value of an existing property by assigning a new value.

let user = {
    name: "John",
    city: "Chennai"
};

user.city = "Coimbatore";

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

Output:

{
    name: "John",
    city: "Coimbatore"
}
Enter fullscreen mode Exit fullscreen mode

Here, the value of city is changed from "Chennai" to "Coimbatore".

Deleting an Object Property

The delete operator is used to remove a property from an object.

let user = {
    name: "John",
    age: 25,
    city: "Chennai"
};

delete user.age;

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

Output:

{
    name: "John",
    city: "Chennai"
}
Enter fullscreen mode Exit fullscreen mode

Here, the age property is removed from the object.

Object with Different Data Types

An object can store values of different data types.

let employee = {
    name: "David",
    age: 28,
    isWorking: true,
    skills: ["HTML", "CSS", "JavaScript"]
};

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

Here, the object contains a String, Number, Boolean, and Array.

Objects Can Contain Functions

An object can also contain a function. A function inside an object is called a method.

let person = {
    name: "John",

    greet: function() {
        console.log("Hello");
    }
};

person.greet();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Here, greet is a method of the person object.

Source: dev.to

arrow_back Back to Tutorials