How to create JavaScript objects using object() constructor?

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 03-Feb-2018 11:23:49
Advertisements