How to create JavaScript objects using object() constructor?


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.

Example

You can try to run the following code to learn how to work with JavaScript objects with object() constructor −

Live Demo

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

Output

Book name is : Perl
Book author is : Tutorialspoint

Updated on: 16-Jun-2020

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements