What is an Object in JavaScript ?
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
// Object literal const obj1 = {name: "Virat" , age: 23} // Object constructor const obj2 = new Object(); obj2.name= "GFG" ; // Object.create() method const obj3 = Object.create(obj2); obj3.name = "Rohit" ; console.log(obj1, obj2, obj3); |
Output
{ name: 'Virat', age: 23 } { name: 'GFG' } { name: 'Rohit' }
Contact Us