1.

Role of === operator in JavaScript

Answer»

LET’s see the following two ways with which you can create an object in JAVASCRIPT:

  • Object creation with constructor METHOD
  • Object creation with Object Literal

Let us understand them one by one:

Object creation with constructor method

To create an object in JavaScript, USE “new” as shown below. It is followed by the constructor method Object():

var department = new Object();

Let us see an example to create an object using Object() constructor:

<html>    <head>       <title>Objects</title>       <script>          var department = new Object();          department.name = "Finance";          department.id  = 005;          department.location = "North";       </script>    </head>    <body>       <h2>Department Details</h2>       <script>          document.write("Department Name = " +  department.name + "<br>");          document.write("Department Id = " + department.id + "<br>");           document.write("Department Location = " + department.location);       </script>    </body> </html>

Above, firstly we have created an object:

var department = new Object();  

After that properties are assigned:

department.name = "Finance"; department.id  = 005; department.location = "North";

Object creation with Object Literal

An object can also be created in JavaScript using object literal as shown below:

var department = {   id: 11,   name : "Finance",   Location     : "North" };

We can write it as:

var department = { id: 11, name : "Finance", Location     : "North"};

Let us see another example:

<!DOCTYPE html> <html> <body> <h2>Department Details</h2> <p id="test"></p> <script> var department = { id: 11, name : "Finance", Location : "North"}; document.getElementById("test").innerHTML = department.name + " Department is in " + department.Location + " location."; </script> </body> </html>

The output: 



Discussion

No Comment Found