Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Published
4 min read
The new Keyword in JavaScript
S
I write code , that run in the browser and someone else's machine. And sometimes I also write articles

Introduction

In JavaScript, the new keyword is used to create objects from constructor functions. It provides a structured way to generate multiple objects that share the same structure and behavior.

To understand new, you first need to understand constructor functions and how object creation works internally.


Constructor Functions

A constructor function is a regular function used to create multiple similar objects. By convention, constructor function names start with a capital letter.

A constructor defines the properties and behavior that each created object should have.

Simple Constructor Example

function Person(name, age) {
  this.name = name;
  this.age = age;
}

This function does not return anything explicitly. Instead, it assigns values to this.

The this keyword refers to the object being created when the function is called with new.


What the new Keyword Does

When you use the new keyword, JavaScript performs several steps automatically behind the scenes.

Consider this example:

const user1 = new Person("Alice", 25);

When new Person("Alice", 25) runs, JavaScript does the following:

Step 1: Creates a New Empty Object

A new empty object is created in memory.

{}

The new object is internally connected to Person.prototype. This allows it to inherit shared methods.

Step 3: Sets this to the New Object

Inside the constructor, this now refers to the newly created object.

this.name = name;
this.age = age;

The properties are added to the new object.

Step 4: Returns the Object

The newly created object is automatically returned unless the constructor explicitly returns another object.

After execution:

console.log(user1);

Output:

Person { name: "Alice", age: 25 }

Object Creation Process Explained Simply

Without using new, you would manually create objects like this:

const user = {
  name: "Alice",
  age: 25
};

If you need many similar objects, repeating this structure becomes inefficient.

Using a constructor with new allows you to define the structure once and create multiple instances:

const user1 = new Person("Alice", 25);
const user2 = new Person("Bob", 30);

Both objects have the same structure but different values.


Every constructor function has a prototype property. Objects created with new are linked to this prototype.

This allows methods to be shared across all instances instead of being duplicated.

Adding a Method to the Prototype

Person.prototype.greet = function() {
  return "Hello, my name is " + this.name;
};

Now every instance created with new Person() can access greet.

console.log(user1.greet());
console.log(user2.greet());

Important idea:

  • The method is not copied into each object.

  • It exists once on the prototype.

  • All instances reference it through the prototype chain.

This makes memory usage more efficient.


Instances Created from Constructors

An object created using new is called an instance of that constructor.

const user1 = new Person("Alice", 25);

Here, user1 is an instance of Person.

You can verify this using:

console.log(user1 instanceof Person); // true

Each instance:

  • Has its own properties defined inside the constructor.

  • Shares methods defined on the constructor’s prototype.

This combination allows both individuality and shared behavior.


Constructor and Object Relationship

The relationship works like this:

Component Role
Constructor Function Blueprint
new Keyword Creates object from blueprint
Instance Created object
Prototype Shared methods and properties

The constructor defines what the object should contain.
The new keyword builds the object.
The instance is the result.


What Happens If You Forget new

If you call a constructor without new, this will not refer to a new object.

const user = Person("Alice", 25);

This can cause unexpected behavior because no new object is created. In strict mode, it may even cause an error.

For this reason, constructors are meant to be used with new.


Final Understanding

The new keyword is used to create objects from constructor functions. It automates object creation by:

  • Creating a new object

  • Linking it to the constructor’s prototype

  • Binding this to the new object

  • Returning the object

Constructor functions act as blueprints, and instances are the objects created from them. The prototype system allows shared behavior without duplicating methods.

Understanding new is essential for understanding object-oriented patterns in JavaScript and how prototypes work behind the scenes.