Site icon i2tutorials

Javascript-new operator

Javascript-new operator

 

This operator lets developers define an instance of user-defined object types. One of the built-in object types which have a constructor function().

 

Syntax:

 

var objectName = new objectType(param1, param2, ...., paramN);

 

Parameters:

 

objectName: Name of the new object.

objectType: The type of the object.

param1, ….paramN : Property values for the object.

 

You want to create a user-defined object type following steps are required.

 

Example:

 

HTML:

 

<head>
<meta charset="utf-8">
<title>JavaScript new operator example.</title>
<meta name="description" content="This document contains an example of JavaScript new operator"/>
</head>
<body>
<script src="javascript-new-operator-example1.js"></script>
</body>

 

JS:

 

var date1 = new Date();
var newParagraph = document.createElement("p");
var newText = document.createTextNode('The date is : '+date1);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);

 

OUTPUT:

 

The date is : Fri Jan 08 2021 12:51:42 GMT+0530 (India Standard Time)

 

 

Exit mobile version