Skip to main content

Command Palette

Search for a command to run...

Understanding the new Keyword in JavaScript

Updated
4 min read

In JavaScript, objects can be created in many ways. One of the most important and commonly used approaches involves the new keyword.

To truly understand how JavaScript creates objects, it is essential to understand how new works internally and how it connects with constructor functions and prototypes.

In this article, we will cover:

  • What the new keyword does

  • Constructor functions

  • The object creation process step by step

  • How new links prototypes

  • Instances created from constructors


What Does the new Keyword Do?

The new keyword is used to create a new object from a constructor function.

Example:

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

const user = new Person("Dipan");

Here, new Person("Dipan") creates a new object and assigns the value "Dipan" to its name property.


Constructor Functions

A constructor function is a regular function used to create multiple objects with similar structure.

By convention, constructor function names start with a capital letter.

Example:

function Car(brand, model) {
  this.brand = brand;
  this.model = model;
}

const car1 = new Car("Toyota", "Corolla");
const car2 = new Car("Honda", "Civic");

Each time we use new, a new object instance is created.


Object Creation Process (Step by Step)

When you use the new keyword, JavaScript performs several steps internally.

Let us break it down:

const user = new Person("Dipan");

Step 1: Create an Empty Object

JavaScript creates a new empty object:

{}

The new object is linked to the constructor’s prototype:

object.__proto__ = Person.prototype

This allows the object to access shared methods.


Step 3: Bind this to the New Object

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

this.name = name;

Step 4: Execute the Constructor Function

The constructor function runs and assigns values to the object.


Step 5: Return the Object

Finally, the newly created object is returned.


Every constructor function has a prototype object.

When an object is created using new, it gets access to that prototype.

Example:

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

Person.prototype.sayHello = function() {
  console.log("Hello, my name is " + this.name);
};

const user = new Person("Dipan");

user.sayHello();

Explanation:

  • sayHello is not inside the object itself

  • It exists on Person.prototype

  • The object can still access it because of the prototype link


Instances Created from Constructors

Objects created using a constructor are called instances.

Example:

function Animal(type) {
  this.type = type;
}

const dog = new Animal("Dog");
const cat = new Animal("Cat");

Here:

  • dog and cat are instances of Animal

You can verify this using instanceof:

console.log(dog instanceof Animal); // true

Each instance:

  • Has its own properties

  • Shares methods through the prototype


Relationship Between Constructor and Object

Let us summarize the relationship:

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

const user = new Person("Dipan");
  • Person → constructor function

  • user → object instance

  • user.__proto__Person.prototype

This connection allows multiple objects to share behavior efficiently.


Conclusion

The new keyword plays a crucial role in JavaScript object creation. It works together with constructor functions and prototypes to create structured and reusable objects.

Key takeaways:

  • new creates a new object from a constructor function

  • It sets up the prototype chain automatically

  • It binds this to the new object

  • Constructor functions help create multiple similar objects

  • Instances share methods through prototypes

Understanding how new works internally gives you deeper insight into JavaScript’s object system and prepares you for advanced concepts like classes and inheritance.