How to create objects in JavaScript?

To create objects, the new operator is used to create an instance of an object. The new operator is followed by the constructor method.

A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable.

The variable contains a reference to the new object. The properties assigned to the object are not variables and are not defined with the var keyword.


You can try to run the following code to learn how to create objects in JavaScript −

Live Demo

      <title>User-defined objects</title>
         var book = new Object(); // Create the object
         book.subject = "Perl"; // Assign properties to the object = "Tutorialspoint";
         document.write("Book name is : " + book.subject + "<br>");
         document.write("Book author is : " + + "<br>");


Book name is : Perl
Book author is : Tutorialspoint