DEV Community

Cover image for Object in JavaScript
Raghul
Raghul

Posted on

Object in JavaScript

Object in JavaScript

An object is a dynamic data structure that stores related data as key-value pairs, where each key uniquely identifies its value. The values of properties can be primitives, objects, or functions (known as methods when defined inside an object). Objects are mutable and dynamic properties can be added, modified, or deleted at any time. Objects allow data grouping and encapsulation, making it easier to manage related information and behaviour together.

How to Create an Object in JavaScript

1.Using Object Literal Syntax
This is the most popular way to create an object in JavaScript. In this way, we create a JavaScript object by enclosing it within curly braces {}.

let laptop = {
  brand: "Apple",
  model: "MacBook Air",
  operatingSystem: "MacOS"
};
Enter fullscreen mode Exit fullscreen mode

The above code snippet creates a JavaScript object named laptop using curly braces. Within the curly braces, three properties are mentioned, separated by commas.

2.Using Object Constructor Function
This way uses the Object constructor along with the new keyword.

let laptop = new Object(); // This creates an empty object

// Now we will add properties to the object
laptop.brand = "Alienware";
laptop.model = "m15";
laptop.operatingSystem = "Windows 11 Home";

console.log(laptop);
//→ {brand: "Alienware", model: "m15", operatingSystem: "Windows 11 Home"}
Enter fullscreen mode Exit fullscreen mode

The above code creates an empty object using new Object(). Then, we add three properties to this empty object. The resultant object is identical to the one created using the object literal syntax.

3.Using the Object.create Method
This method is used when we wish to inherit properties from an existing object while creating a new object.

let animal = {
  diet: null,
  saySomething: function(word) { console.log(word); }
};

let cat = Object.create(animal);

console.log(cat.diet);
//→ null

cat.saySomething("Meeow");
//→ Meeow
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, the object cat inherits the properties diet and saySomething() from the object animal while the former object is being created. The object animal acts as the parent object, and cat is the child object. Changing the child property diet’s value does not affect the corresponding property for the parent object.

4.Using Object.assign Method
This method is used when we wish to include properties from multiple other objects into the new object we are creating.

let lesson = {
  lessonName: "Data Structures"
};

let teacher = {
  teacherName: "Himmat Singh"
};

let course = Object.assign({}, lesson, teacher);

console.log(course);
//→ {lessonName: "Data Structures", teacherName: "Himmat Singh"}
Enter fullscreen mode Exit fullscreen mode

In the above example, we create a new object named course and include all properties from two objects — lesson and teacher — into it. The properties from the source objects get copied to the target object {}, resulting in the object variable course.

Top comments (0)