Lesson 28 of 30 – JavaScript New
93%

JavaScript New Keyword

The new keyword is used to create an object from a constructor function. It automatically creates and returns a new object instance.

Note: The new keyword creates a new object, binds this to that object and returns it.
Introduction to new Keyword

Objects can be created using the new keyword and a constructor function.

function MyFunc(){
this.x = 100;
}

var obj1 = new MyFunc();
Constructor Function

A function used with the new keyword is called a constructor function.

function MyFunc(){
this.x = 100;
}

MyFunc acts as a constructor when called using new.

Built-in Constructor Functions

JavaScript provides several built-in constructor functions.

  • Object()
  • Array()
  • String()
  • Number()
  • Boolean()
Example of Built-in Function
Object

Output:
function Object()

Built-in data types are internally implemented as functions.

Object Creation Example
function MyFunc(){
this.x = 100;
}

var obj1 = new MyFunc();
alert(obj1.x);
Output:

100

What Does new Do?

The new keyword performs several operations automatically while creating an object.

Step 1 – Create Empty Object

The first task of the new keyword is to create a brand new empty object.

var obj = {};
Step 2 – Connect Prototype

The prototype of the constructor function is linked with the newly created object.

MyFunc.prototype.y = 200;
Step 3 – Bind this to New Object

The new keyword binds all properties and methods declared with the this keyword to the newly created object.

function MyFunc(){
this.x = 100;
this.name = "Aman";
}

Properties x and name become part of the new object.

Step 4 – Return New Object

Finally, JavaScript returns the newly created object automatically.

function MyFunc(){
this.x = 100;
}

var obj = new MyFunc();

JavaScript internally adds return this; if no return statement exists.

Prototype Example
function MyFunc(){
this.x = 100;
}

MyFunc.prototype.y = 200;

var obj1 = new MyFunc();
Accessing Prototype Properties
alert(obj1.x);
alert(obj1.y);
Output:

100
200

x comes from the object and y comes from the prototype.

Local Variables Are Not Added

Variables declared without this remain private to the function.

function MyFunc(){
var myVar = 1;
this.x = 100;
}

Only x becomes part of the object.

Primitive Return Value

If a constructor returns a primitive value, the new keyword ignores it.

function MyFunc(){
this.x = 100;
return 200;
}

var obj = new MyFunc();
Output of Primitive Return
alert(obj.x);
Output:

100

The primitive value 200 is ignored.

Non-Primitive Return Value

If a constructor returns an object, JavaScript returns that object instead of the newly created instance.

function MyFunc(){
this.x = 100;

return { a:123 };
}
Output of Non-Primitive Return
var obj1 = new MyFunc();
alert(obj1.x);
Output:

undefined

Because the constructor returns a custom object, the newly created object is discarded.

Difference Between Primitive and Object Return
Return Type Behavior
Primitive Value Ignored by new keyword
Object Value Returned instead of new object
Advantages of new Keyword
  • Creates objects automatically.
  • Binds this to the new object.
  • Supports constructor functions.
  • Provides prototype inheritance.
  • Improves code reusability.
Four Tasks Performed by new
  1. Create a new empty object.
  2. Link prototype to constructor prototype.
  3. Bind this to the new object.
  4. Return the newly created object.
Key Features of new Keyword
Feature Description
Constructor Creates object instances
Prototype Supports inheritance
this Binding Binds current object
Automatic Return Returns object instance
Points to Remember
  • The new keyword creates an object instance.
  • Constructor functions are called using new.
  • Properties declared with this become object properties.
  • Prototype properties are inherited automatically.
  • Primitive return values are ignored.
  • Object return values replace the created object.
  • new automatically returns the created object.

🧠 Quick Quiz

Which of the following is the first task performed by the new keyword?