Javascript Object – How to create an Object

Front-End Interview

There are two ways you can create Javascript object.

1 Using an Object Literal

An object literal is a list of key/val pairs inside curly braces {}.

Here is the example to create a new JavaScript object with four properties:

Example

let person = {};

person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

2 Using the JavaScript Keyword new

You can also create a new javascript object using new keyword.

Example

let person = new Object();

person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";