Skip to main content

Command Palette

Search for a command to run...

JavaScript new Keyword Explained: Constructor Functions, Prototypes & Object Creation

Updated
β€’4 min readβ€’View as Markdown
JavaScript new Keyword Explained: Constructor Functions, Prototypes & Object Creation

JavaScript is a powerful language, and one of its most important concepts in object-oriented programming is the new keyword.

If you've ever wondered how objects are created behind the scenes, this guide will give you a clear, step-by-step understanding of how new works.


πŸ“Œ What is the new Keyword?

The new keyword in JavaScript is used to create an instance of an object from a constructor function.

πŸ‘‰ In simple terms: It helps you create multiple objects using a blueprint.


🧱 Constructor Functions

A constructor function is a regular function used to create objects.

Example:

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

Using new:

const user1 = new Person("Vishal", 20);
const user2 = new Person("Rahul", 22);

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

Output:

Person { name: "Vishal", age: 20 }
Person { name: "Rahul", age: 22 }

πŸ‘‰ Each object is a separate instance created from the same constructor.


βš™οΈ What Happens When You Use new?

When you write:

const user = new Person("Vishal", 20);

JavaScript performs 4 hidden steps:


πŸ”Ή Step 1: Create an empty object

const obj = {};

obj.__proto__ = Person.prototype;

πŸ”Ή Step 3: Bind this to the new object

Person.call(obj, "Vishal", 20);

πŸ”Ή Step 4: Return the object

return obj;

πŸ‘‰ Final result:

const user = {
    name: "Vishal",
    age: 20
};

πŸ”„ Object Creation Flow

Constructor Function 
        ↓
     new keyword
        ↓
  Empty Object {}
        ↓
  Prototype Linked
        ↓
   this Binding
        ↓
   Final Object Returned

Every constructor function has a prototype property.

Example:

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

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

const user = new Person("Vishal");
user.greet(); // Hello, Vishal

πŸ‘‰ This works because:

user.__proto__ === Person.prototype

🧬 Prototype Chain

user β†’ Person.prototype β†’ Object.prototype β†’ null

πŸ‘‰ This is called the prototype chain.


πŸ‘₯ Instances Created from Constructors

Each time you use new, a new instance is created.

Example:

const p1 = new Person("A");
const p2 = new Person("B");

console.log(p1 === p2); // false

πŸ‘‰ Even though both use the same constructor, they are different objects.


❌ What Happens Without new?

const user = Person("Vishal", 20);
console.log(user); // undefined

πŸ‘‰ Problem:

  • this does not refer to a new object

  • In strict mode, it becomes undefined


βœ… Best Practice

Use new with constructor functions or prefer modern classes:

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

🧩 Real-World Analogy

Think of a constructor as a blueprint of a house 🏠

  • Constructor β†’ Blueprint

  • new β†’ Building the house

  • Instance β†’ Actual house


πŸ”₯ Final Summary

βœ” new creates a new object βœ” Links it to prototype βœ” Binds this βœ” Returns the object


🎯 Conclusion

Understanding the new keyword is essential for mastering JavaScript’s object system.

Once you understand:

  • Constructor functions

  • Prototype linking

  • Instance creation

πŸ‘‰ You gain deep insight into how JavaScript works internally.


More from this blog

Dev Blog by Vishal

37 posts