JavaScript New Keyword

Shiv Yadav Oct 12, 2023
  1. What Is the new Keyword in JavaScript
  2. Use the new Keyword in JavaScript
JavaScript New Keyword

This article helps you understand the use of the new keyword in JavaScript.

What Is the new Keyword in JavaScript

JavaScript’s new keyword is used to instantiate an object using a constructor. The following happens when the constructor is called with the new operator.

  1. A new empty object is generated.

  2. The new object’s internal Prototype property is the same as the constructor prototype.

  3. The variable this is designed to point to the newly created object. It binds the property declared with the this keyword to the new object.

  4. When the constructor returns a non-primitive value (custom JavaScript object), a newly created object is returned. If the constructor returns a primitive value, it is disregarded.

    After the function, this is returned if there are no return statements in the function body.

Syntax:

new constructorFunction(arguments);

Parameter:

  1. ConstructorFunction - A class or function that specifies the type of the object instance.
  2. Arguments - List of values on which the constructor will be called.

Use the new Keyword in JavaScript

Example 1:

function Book(name, price, pages) {
  this.name = name;
  this.price = price;
  this.pages = pages;
}

const book1 = new Book('Science', 20, 480);

document.write(book1.name);

Try Demo.

Output:

Science

The new keyword creates an empty object in the above example. Book() includes three properties: name, price, and pages notified with this term.

As a result, a new empty object will have all of these properties, i.e., name, price, and pages. The newly created things are returned as book1().

Example 2:

function func() {
  var as = 1;
  this.s = 500;
}

func.prototype.k = 1000;

var obj = new func();
document.write(obj.s);
document.write('\n');
document.write(obj.k);

Try Demo.

Output:

500 1000

The new keyword in the above example creates an empty object and then sets the prototype attribute to func()’s prototype property. New property k is assigned using func.prototype.k.

So, the new entity will also include the k property; then, it binds all the properties and functions declared with the this keyword to a new empty object.

Here, func() includes only one property s, expressed with the this keyword. So a new open entity will now have s property.

The func() includes the as variable, not declared with the this keyword. So as will not be included in a new object.

Lastly, the newly created object is returned. Note that func() does not have a return statement.

The compiler will implicitly insert return this at the end.

Author: Shiv Yadav
Shiv Yadav avatar Shiv Yadav avatar

Shiv is a self-driven and passionate Machine learning Learner who is innovative in application design, development, testing, and deployment and provides program requirements into sustainable advanced technical solutions through JavaScript, Python, and other programs for continuous improvement of AI technologies.

LinkedIn

Related Article - JavaScript Keyword