CS Engineering Gyan

JavaScript Objects

Objects are one of the most important concepts in JavaScript. They allow developers to store related data and functionality together in a structured way.

Unlike arrays that mainly store collections of values, objects store data in the form of key-value pairs. Each key represents a property name and each value represents the data associated with that property.

Objects are widely used in modern web applications to represent real-world entities such as users, products, students, employees, accounts, and many other things.


What is an Object in JavaScript?

An object is a collection of related information stored as properties and behaviors stored as methods.

A property contains data, while a method contains a function that performs an action.

Example


let student = {

    name:"Rahul",

    age:20,

    course:"Computer Science"

};

In this example, student is an object containing three properties: name, age, and course.


Why are Objects Important?

Objects help developers organize complex information in a meaningful structure. They make programs easier to understand, maintain, and expand.

Advantages of Objects


Creating Objects in JavaScript

JavaScript provides multiple ways to create objects.


1. Object Literal Method

The object literal method is the simplest and most commonly used way to create objects.

Syntax


let objectName = {

    property:value

};

Example


let car = {

    brand:"Toyota",

    model:"Fortuner",

    year:2025

};


document.write(car.brand);

Output


Toyota


2. Using new Object()

JavaScript provides the Object constructor to create objects.

Example


let person = new Object();


person.name = "Amit";

person.age = 25;


document.write(person.name);

Output


Amit


3. Creating Objects Using Constructor Function

Constructor functions allow developers to create multiple objects with the same structure.

Example


function Student(name,age)
{

    this.name = name;

    this.age = age;

}


let s1 = new Student("Rahul",20);


document.write(s1.name);

Output


Rahul


Object Properties in JavaScript

Properties are values associated with an object. They describe the characteristics of an object.

Example


let mobile = {

    brand:"Samsung",

    price:30000,

    color:"Black"

};

Property Value
brand Samsung
price 30000
color Black

Accessing Object Properties

JavaScript provides two ways to access object properties.


1. Dot Notation

Dot notation is the most common way to access object values.

Example


let student = {

name:"Ravi",

age:21

};


document.write(student.name);

Output


Ravi


2. Bracket Notation

Bracket notation is useful when property names are stored in variables or contain special characters.

Example


let student = {

name:"Ravi",

age:21

};


document.write(student["age"]);

Output


21


Adding New Properties to Objects

New properties can be added to existing objects at any time.

Example


let person = {

name:"Amit"

};


person.age = 25;


document.write(person.age);

Output


25


Updating Object Properties

Existing object values can be modified by assigning a new value.

Example


let product = {

name:"Laptop",

price:50000

};


product.price = 60000;


document.write(product.price);

Output


60000


Deleting Object Properties

The delete keyword removes a property from an object.

Example


let user = {

name:"Rahul",

age:20

};


delete user.age;


document.write(user.name);

Output


Rahul



Object Methods in JavaScript

A method is a function that is stored as a property of an object. Object methods are used to perform actions related to the data stored inside an object.

Methods help combine data and functionality together, making objects more powerful and useful.

Example: Object Method


let student = {

    name:"Rahul",

    greet:function()
    {

        document.write("Hello " + this.name);

    }

};


student.greet();

Output


Hello Rahul

In this example, greet() is a method of the student object.


The this Keyword in JavaScript Objects

The this keyword refers to the current object that is executing the method.

It allows a method to access properties and other methods of the same object.

Example


let employee = {

    name:"Amit",

    salary:50000,


    display:function()
    {

        document.write(this.name);

    }

};


employee.display();

Output


Amit

Here, this.name refers to the name property of the employee object.


Object with Multiple Methods

An object can contain multiple methods to perform different operations.

Example


let calculator = {

    add:function(a,b)
    {

        return a+b;

    },


    multiply:function(a,b)
    {

        return a*b;

    }

};


document.write(calculator.add(10,20));

Output


30


Nested Objects in JavaScript

A nested object is an object that contains another object as one of its properties.

Nested objects are useful for representing complex information in a structured format.

Example


let student = {

    name:"Rahul",


    address:
    {

        city:"Delhi",

        country:"India"

    }

};


document.write(student.address.city);

Output


Delhi


Objects Inside Arrays

Arrays can store multiple objects. This structure is commonly used when handling collections of data.

Example


let users = [

{

name:"Amit",

age:25

},


{

name:"Neha",

age:22

}

];


document.write(users[1].name);

Output


Neha

Arrays of objects are frequently used when working with API data, databases, and applications.


Accessing Object Properties Dynamically

JavaScript allows properties to be accessed dynamically using variables with bracket notation.

Example


let student = {

name:"Ravi",

marks:90

};


let property = "marks";


document.write(student[property]);

Output


90


Object.keys() Method

The Object.keys() method returns an array containing all property names of an object.

Example


let person = {

name:"Rahul",

age:20,

city:"Mumbai"

};


let result = Object.keys(person);


document.write(result);

Output


name,age,city


Object.values() Method

The Object.values() method returns an array containing all values of an object.

Example


let product = {

name:"Laptop",

price:50000,

brand:"Dell"

};


let result = Object.values(product);


document.write(result);

Output


Laptop,50000,Dell


Object.entries() Method

The Object.entries() method converts object properties into an array of key-value pairs.

Example


let student = {

name:"Amit",

marks:85

};


let result = Object.entries(student);


document.write(result);

Output


name,Amit,marks,85


Looping Through Objects

The for...in loop is commonly used to iterate through object properties.

Example


let mobile = {

brand:"Samsung",

price:30000,

color:"Black"

};


for(let key in mobile)
{

    document.write(key);

}

Output


brand

price

color


Accessing Values Using for...in Loop


let user = {

name:"Rahul",

age:25,

city:"Pune"

};


for(let key in user)
{

    document.write(user[key]);

}

Output


Rahul

25

Pune


Object.assign() Method

The Object.assign() method is used to copy properties from one or more objects into another object.

Example


let first = {

name:"Rahul"

};


let second = {

age:20

};


let result = Object.assign(first,second);


document.write(result.name);

Output


Rahul


Cloning Objects in JavaScript

Creating a copy of an object is called cloning. JavaScript provides different methods for cloning objects.

Using Spread Operator


let user = {

name:"Amit",

age:22

};


let copy = {...user};


document.write(copy.name);

Output


Amit


Using Object.assign() for Cloning


let original = {

name:"Neha",

city:"Delhi"

};


let clone = Object.assign({},original);


document.write(clone.city);

Output


Delhi


Checking Object Properties

The hasOwnProperty() method checks whether an object contains a specific property.

Example


let student = {

name:"Ravi",

age:21

};


document.write(student.hasOwnProperty("name"));

Output


true


Freezing Objects

The Object.freeze() method prevents modification of an object.

Example


let user = {

name:"Rahul"

};


Object.freeze(user);


user.name="Amit";


document.write(user.name);

Output


Rahul

After freezing, properties cannot be changed, added, or deleted.


Sealing Objects

The Object.seal() method prevents adding or deleting properties but allows updating existing properties.

Example


let product = {

name:"Mobile",

price:20000

};


Object.seal(product);


product.price=25000;


document.write(product.price);

Output


25000



Object Constructor in JavaScript

An object constructor is a special type of function used to create multiple objects with the same structure. It works like a blueprint for creating similar objects.

Constructor functions are useful when an application needs many objects having the same properties and methods.

Syntax


function ObjectName(property1, property2)
{

    this.property1 = property1;

    this.property2 = property2;

}

Example: Constructor Function


function Student(name, age, course)
{

    this.name = name;

    this.age = age;

    this.course = course;

}


let student1 = new Student("Rahul",20,"B.Tech");


document.write(student1.name);

Output


Rahul

The new keyword creates a new object from the constructor function.


Adding Methods in Constructor Functions

Methods can also be added inside constructor functions to provide behavior to objects.

Example


function Employee(name,salary)
{

    this.name = name;

    this.salary = salary;


    this.display = function()
    {

        document.write(this.name);

    }

}


let emp = new Employee("Amit",50000);


emp.display();

Output


Amit


JavaScript Classes and Objects

JavaScript classes provide a modern and cleaner way to create objects. Classes were introduced in ES6 and are based on prototype inheritance.

A class acts as a template from which multiple objects can be created.

Syntax


class ClassName
{

    constructor()
    {

    }

}


Example: Creating Class


class Student
{

    constructor(name,age)
    {

        this.name = name;

        this.age = age;

    }


    display()
    {

        document.write(this.name);

    }

}


let s1 = new Student("Neha",21);


s1.display();

Output


Neha


Constructor Method in Classes

The constructor method is automatically called when a new object is created from a class.

It is mainly used to initialize object properties.

Example


class Car
{

    constructor(brand)
    {

        this.brand = brand;

    }

}


let car1 = new Car("BMW");


document.write(car1.brand);

Output


BMW


Object Inheritance in JavaScript

Inheritance allows one object or class to access properties and methods of another object or class.

It helps reduce duplicate code and improves code reusability.

Example Using extends


class Animal
{

    sound()
    {

        document.write("Animal Sound");

    }

}


class Dog extends Animal
{


}


let d = new Dog();


d.sound();

Output


Animal Sound


Prototype in JavaScript

Every JavaScript object has a prototype. A prototype is an object from which other objects can inherit properties and methods.

JavaScript uses prototype-based inheritance internally.

Example


function Person(name)
{

    this.name = name;

}


Person.prototype.show = function()
{

    document.write(this.name);

};


let p1 = new Person("Rahul");


p1.show();

Output


Rahul


Getter Methods in JavaScript

A getter is a method that allows accessing an object property like a normal property.

Getters are created using the get keyword.

Example


let student = {

name:"Amit",


get getName()
{

    return this.name;

}

};


document.write(student.getName);

Output


Amit


Setter Methods in JavaScript

A setter allows updating object properties using a special method.

Setters are created using the set keyword.

Example


let person = {

name:"Rahul",


set changeName(value)
{

    this.name = value;

}

};


person.changeName = "Amit";


document.write(person.name);

Output


Amit


Object Destructuring in JavaScript

Object destructuring allows extracting object properties into separate variables using a simple syntax.

Example


let student = {

name:"Ravi",

age:20

};


let {name,age} = student;


document.write(name);

Output


Ravi


Spread Operator with Objects

The spread operator can be used with objects to copy or combine object properties.

Example: Copy Object


let user = {

name:"Rahul",

age:25

};


let copy = {...user};


document.write(copy.name);

Output


Rahul


Combining Objects Using Spread Operator


let first = {

name:"Amit"

};


let second = {

city:"Delhi"

};


let result = {

...first,

...second

};


document.write(result.city);

Output


Delhi


JSON Objects in JavaScript

JSON stands for JavaScript Object Notation. It is a lightweight format used for storing and exchanging data between applications.

JSON data is commonly used when communicating with servers and APIs.

Example JSON Object


{

"name":"Rahul",

"age":20,

"course":"Computer Science"

}


Converting Object to JSON

The JSON.stringify() method converts a JavaScript object into a JSON string.

Example


let student = {

name:"Amit",

age:21

};


let data = JSON.stringify(student);


document.write(data);

Output


{"name":"Amit","age":21}


Converting JSON to Object

The JSON.parse() method converts JSON data into a JavaScript object.

Example


let data = '{"name":"Rahul","age":22}';


let student = JSON.parse(data);


document.write(student.name);

Output


Rahul


Real-World Applications of JavaScript Objects

Objects are used extensively in modern web applications because they represent real-world data efficiently.


Common Mistakes While Using Objects


Best Practices for JavaScript Objects


JavaScript Objects Interview Questions

  1. What is an object in JavaScript?
  2. What are properties and methods?
  3. How can you create objects in JavaScript?
  4. What is the difference between object and array?
  5. Explain the this keyword.
  6. What are constructor functions?
  7. What are JavaScript classes?
  8. Explain prototype inheritance.
  9. What is object destructuring?
  10. What is JSON?
  11. Difference between JSON.stringify() and JSON.parse().
  12. What are getters and setters?
  13. Explain Object.keys() and Object.values().
  14. How can you clone an object?
  15. What is object inheritance?

Summary

JavaScript objects are powerful structures used to store and manage related information in the form of properties and methods. Objects make programs organized, reusable, and easier to maintain.

Important object concepts include object creation, properties, methods, constructors, classes, inheritance, prototypes, destructuring, JSON handling, and advanced object operations.

A strong understanding of objects is essential for advanced JavaScript development, including DOM manipulation, APIs, frameworks, and full-stack web applications.


← Previous: JavaScript Arrays Next: JavaScript Events →
Home Visit Our YouTube Channel