Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript — From Confusion to Clarity.

Updated
•6 min read•View as Markdown
The new Keyword in JavaScript — From Confusion to Clarity.

If you've ever written JavaScript like this:

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

const u1 = new User("Chitta");

…and thought:

"What exactly is new doing behind the scenes?"

You're not alone.

Most developers use new — but very few truly understand it deeply.

This blog will break it down in a step-by-step, visual, and practical way so that you can confidently explain it in interviews and use it in real-world systems.


Why the new Keyword Matters

Before understanding how, let’s understand why.

Without new, JavaScript objects would be harder to structure and reuse.

new helps you:

  • Create multiple objects from a template

  • Use object-oriented patterns

  • Reuse logic efficiently

  • Link objects with shared behavior (via prototypes)


What the new Keyword Does (In One Line)

The new keyword creates a new object and connects it to a constructor function and its prototype.

But that's just the surface.

Let’s break it down properly.


Step-by-Step: What Happens When You Use new

When you run:

const user = new User("Chitta");

JavaScript internally performs 4 steps:


Step 1: Create an Empty Object

const obj = {};

obj.__proto__ = User.prototype;

This is the most important step.


Step 3: Bind this

User.call(obj, "Chitta");

Now inside the constructor:

this.name = "Chitta";

Step 4: Return the Object

return obj;

Unless you explicitly return another object.


Visual Flow of new

Image Image Image Image Image Image

Constructor Functions (The Foundation)

A constructor is just a regular function, but with a convention:

  • Starts with a capital letter

  • Uses this to assign properties

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

Creating Instances

const car1 = new Car("Tesla", "Model 3");
const car2 = new Car("BMW", "X5");

Each call creates a new independent object.


Important Insight

Without new, the same function behaves completely differently.

const car = Car("Tesla", "Model 3");
console.log(car); // undefined

Why?

Because:

  • this will point to global object (or undefined in strict mode)

  • No object is created


Understanding Prototype Linking

This is where things get powerful.

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

User.prototype.sayHello = function () {
  console.log("Hello " + this.name);
};
const u1 = new User("Chitta");
u1.sayHello(); // Hello Chitta

What’s Happening?

  • u1 does NOT have sayHello

  • JavaScript looks into User.prototype

  • Finds the method there


Prototype Linking Visual

Image Image Image Image Image Image Image

Key Takeaway

new connects your object to shared methods using prototypes.

This is memory efficient.

Instead of duplicating methods:

  • One method → shared across all instances

Instance Creation Deep Dive

Let’s inspect:

console.log(u1);

Output:

User { name: "Chitta" }

Now check:

console.log(u1.__proto__ === User.prototype); // true

Constructor vs Instance Relationship

Concept Meaning
Constructor Blueprint function
Instance Actual object created
Prototype Shared methods storage

Internal Working Simulation

Let’s recreate new manually:

function myNew(constructor, ...args) {
  const obj = {};
  obj.__proto__ = constructor.prototype;

  const result = constructor.apply(obj, args);

  return result instanceof Object ? result : obj;
}

Usage

const u2 = myNew(User, "Rahul");
console.log(u2);

Now you understand:

new is not magic — it’s just a structured process.


Constructor Returning Values (Edge Case)

function Test() {
  this.name = "Default";
  return { name: "Override" };
}

const t = new Test();
console.log(t.name); // Override

Rule

  • If constructor returns object → that object is returned

  • If it returns primitive → ignored


Real-World Use Case

You’ll see new used in:

  • Object-oriented systems

  • Libraries

  • Design patterns

  • Older JavaScript codebases

Example:

const date = new Date();

Behind the Scenes (Animation Understanding)

Image Image Image Image Image Image

Common Mistakes

1. Forgetting new

const user = User("Chitta"); // wrong

2. Using Arrow Functions as Constructors

const User = (name) => {
  this.name = name;
};

new User("Chitta"); // ❌ Error

Arrow functions:

  • Do NOT have this

  • Cannot be used with new


3. Overusing Constructors Instead of Classes

Modern JavaScript prefers:

class User {
  constructor(name) {
    this.name = name;
  }
}

But internally — it still uses new.


Advanced Insight (Interview Level)

  • Internal [[Prototype]]

  • constructor reference

  • Dynamic this binding


Prototype Chain Lookup

When you do:

u1.sayHello()

JS checks:

  1. Inside u1

  2. Then u1.__proto__

  3. Then higher chain


Mental Model (Most Important)

Think like this:

Constructor = Blueprint Prototype = Shared Brain Instance = Real Object new = Factory Machine


Final Summary

  • new creates a fresh object

  • Links it to a constructor’s prototype

  • Binds this to that object

  • Returns the object automatically


One-Line Definition (Interview Ready)

The new keyword creates an object, links it to a constructor’s prototype, binds this, and returns the instance.


Closing Thought

Most developers memorize new.

Top developers understand it deeply.

Once you get this:

  • Prototypes become easy

  • Classes make sense

  • JavaScript feels less "weird"

More from this blog