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 = {};
πΉ Step 2: Link the object to prototype
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
π How new Links Prototypes
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:
thisdoes not refer to a new objectIn 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 houseInstance β 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.