What is an Object in JavaScript ?
Last Updated :
29 Jan, 2024
An object in JavaScript is a non-primitive data type that is used to store multiple values of different primitive data types in the form of key-value pairs.
A JavaScript object can be created using the following ways:
- Using Object literal: it is the most common way to create objects in JavaScript. It involves the use of the curly brackets({}) to create objects.
- Using Object Constructor: It is an in-built constructor that can be used to create JavaScript objects.
- Using Object.create() method: Object.create() is also an in-built method used to create objects.
Example: The below code implements the above approaches to create an object in JavaScript.
Javascript
const obj1 = {name: "Virat" , age: 23}
const obj2 = new Object();
obj2.name= "GFG" ;
const obj3 = Object.create(obj2);
obj3.name = "Rohit" ;
console.log(obj1, obj2, obj3);
|
Output
{ name: 'Virat', age: 23 } { name: 'GFG' } { name: 'Rohit' }
Share your thoughts in the comments
Please Login to comment...