Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Published
3 min read
The new Keyword in JavaScript

In this article we'll learn about another magical keyword of JavaScript, the new keyword. The new keyword is used to create objects from constructor functions. Instead of manually creating an object every time, new helps automate the process and gives you a clean way to create multiple similar objects.

At a high level, when you use new, JavaScript does a few things behind the scenes:

  • creates a brand new empty object

  • links that object to a prototype

  • runs the constructor function with this pointing to that new object

  • returns the object

This is why new is so powerful — it bundles multiple steps into one simple line.

Constructor functions

A constructor function is just a normal function, but by convention it starts with a capital letter and is meant to be used with new.

Example:

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

This function doesn’t return anything explicitly, but when used with new, it will create and return an object.

Object creation process

Let’s say we do this:

const user1 = new User("Alex", 25);

Here’s what actually happens step by step:

  1. JavaScript creates an empty object:
{}
  1. It sets the prototype of this object to User.prototype

  2. It calls the constructor:

User.call(newObject, "Alex", 25);

So inside the function:

this.name = "Alex";
this.age = 25;
  1. Finally, it returns the object

So user1 becomes:

{
  name: "Alex",
  age: 25
}

This is where things get interesting.

Every function in JavaScript has a prototype property. When you use new, the created object is internally linked to that prototype.

User.prototype.greet = function () {
  console.log("Hello " + this.name);
};

Now:

const user1 = new User("Alex", 25);
user1.greet();

Even though greet is not inside the object itself, JavaScript looks up the prototype chain and finds it.

So the link looks like this:

user1 -> User.prototype -> Object.prototype

This is called the prototype chain.

Instances created from constructors

Each time we use new, we create a new instance.

const user1 = new User("Alexa", 25);
const user2 = new User("Sam", 30);

Both are separate objects:

user1 !== user2

But they share the same prototype:

user1.__proto__ === user2.__proto__

This is efficient because methods are not duplicated for every object -- they live in one place (the prototype).

Final understanding

The new keyword is basically a shortcut for creating objects in a structured way. It handles object creation, sets up the prototype chain, binds this, and returns the object automatically. Constructor functions define the blueprint, and each call with new creates a new instance that follows that blueprint while sharing common behaviour through prototypes.