What is the 'new' keyword in JavaScript to create an object and how to access values?

The new keyword creates an object in JavaScript. To access values, use the dot notation. Following is the code −


function newObjectCreation() {
   this.firstName = "John";
var newObject = new newObjectCreation();
console.log("The first name="+newObject.firstName);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo134.js


This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo134.js
The first name=David