The new Keyword in JavaScript
Introduction
In JavaScript, there are multiple ways to create objects. One of the most important methods is using constructor functions along with the new keyword.
If you’ve ever written:
const user = new User("Parth");
Then the new keyword is doing a lot of work behind the scenes.
In this article, we’ll understand:
What the new keyword does Constructor functions How object creation works internally How new links prototypes What instances are What the new Keyword Does
The new keyword is used to create a new object from a constructor function.
In simple terms:
👉 new = "Create a new object and set it up"
Constructor Functions
A constructor function is just a regular function, but by convention, its name starts with a capital letter.
Example: function User(name, age) { this.name = name; this.age = age; }
This function does not explicitly return anything, but when used with new, it creates and returns an object.
Object Creation Using new const user1 = new User("Parth", 21);
This single line performs multiple steps internally.
Step-by-Step: What Happens Internally
When you write:
const user1 = new User("Parth", 21); Step 1: A new empty object is created const obj = {}; Step 2: this is bound to the new object this = obj; Step 3: The constructor function executes this.name = "Parth"; this.age = 21;
Now the object looks like:
{ name: "Parth", age: 21 } Step 4: Prototype linking happens obj.proto = User.prototype;
👉 This is a very important concept.
Step 5: The object is returned return obj; Final Object { name: "Parth", age: 21 } Prototype Linking Explained
Every function in JavaScript has a prototype property.
User.prototype.greet = function () { console.log("Hello " + this.name); };
Now:
const user1 = new User("Parth", 21); user1.greet();
👉 This works because:
user1.proto is linked to User.prototype Conceptual Flow Diagram new User("Parth", 21)
↓
Step 1 Create empty object {}
Step 2 Bind this → {}
Step 3 Add properties
Step 4 Link to User.prototype
Step 5 Return object Instances Created from Constructors
const user1 = new User("Parth",21);
const user2 = new User("Rahul", 22);
Both are instances of the User constructor.
Key Point: Each instance has its own data All instances share methods via the prototype Constructor vs Object Constructor Function Object Blueprint Actual instance Defines structure Holds data Has prototype Linked to prototype Without new (Common Mistake ) const user1 = User("Parth", 21);
Problem:
this may refer to the global object Object is not created correctly Best Practices
✔ Always use new with constructor functions ✔ Use capitalized names for constructors ✔ Define methods on the prototype (memory efficient)
Conclusion
The new keyword in JavaScript is a powerful feature that:
Creates a new object Binds this to that object Links the object to the constructor’s prototype Returns the final object
Understanding this is essential for mastering JavaScript’s object-oriented behavior.