Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Updated
•4 min read•View as Markdown
The new Keyword in JavaScript

Imagine JavaScript as a factory.
In this factory, we don’t create objects randomly. We follow a proper process, and the new keyword is the manager that supervises this process.

Today, we’ll understand:

  • What new actually does

  • What constructor functions are

  • How objects are created step by step

  • How prototypes are linked

  • How instances are formed

By the end, you’ll see object creation in your mind, not just memorize code.


What Does the new Keyword Do?

Let’s start simple.

When you write this:

const user = new User("Asad", 22);

You might think:

“Okay, it creates an object.”

But how?

What happens inside JavaScript?

In simple words:

👉 new creates a fresh empty object and connects it to a constructor.

Think of new as saying:

“Hey JavaScript, please create a brand-new object using this blueprint.”

That blueprint is called a constructor function.


Constructor Functions (The Blueprint)

What is a constructor?

A constructor function is just a normal function, but:

  • It is written with a capital letter (by convention)

  • It is meant to create objects

Example:

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

Think of this like a form template:

  • Every user will have a name

  • Every user will have an age

But right now…
❌ No object is created
❌ No memory is used

It’s just a plan, not the real thing.


Object Creation Process

Now comes the heart of the topic

Let’s see what happens when we use new.

const user1 = new User("Asad", 22);

Behind the scenes, JavaScript does 4 steps automatically.


🔹 Step 1: A New Empty Object Is Created

JavaScript secretly creates {}:

{}

You don’t see it, but it exists in memory.


🔹 Step 2: this Points to That New Object

Inside the constructor:

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

Now this is not random.

👉 this = newly created object So it becomes:

{
  name: "Asad",
  age: 22
}

🔹 Step 3: Prototype Is Linked Automatically

This is where beginners usually get confused 😅
So let’s slow down.

JavaScript connects the object to:

User.prototype

Like this (conceptually):

user1.__proto__ === User.prototype

Why is this important?

Because methods live in prototypes, not inside every object.


🔹 Step 4: Object Is Returned Automatically

You never write return
JavaScript does it for you.

So finally:

const user1 = {
  name: "Asad",
  age: 22
};

🎉 Object created successfully.


Let’s add a method.

User.prototype.sayHi = function () {
  return `Hi, my name is ${this.name}`;
};

Now check:

user1.sayHi(); 

Question:

Where is sayHi stored?

Answer:
❌ Not inside user1
✅ Inside User.prototype

JavaScript says:

“If property not found in object, look in prototype.”

This is called the Prototype Chain.


Instances Created from Constructors

Every time you use new, you create a new instance.

const user1 = new User("Asad", 22);
const user2 = new User("Ali", 25);

Both are:

  • Created from the same constructor

  • Linked to the same prototype

  • But are different objects in memory

user1 === user2 // false

Think like this:

  • Constructor = Factory machine

  • Instances = Products coming out of it


Relation Between Constructor and Object

Thing Role
Constructor Blueprint
new Builder
Object Final Product
Prototype Shared behavior

One constructor
Many objects

One shared prototype


Why new Is Important

Without new:

  • this breaks

  • Prototype linking doesn’t happen

  • Object isn’t created properly

That’s why new is not optional with constructor functions.


Final Mental Picture (Remember This)

Whenever you see:

new Something()

Immediately think:

1️⃣ Empty object created
2️⃣ this points to it
3️⃣ Prototype is linked
4️⃣ Object is returned

If you remember this flow,
👉 you already understand new better than most beginners