The new Keyword in JavaScript

When you're learning JavaScript, one of the first confusing things is how objects are actually created.
And then you see this small keyword — new.
It looks simple, but it does a lot behind the scenes.
Let’s understand it in a way that actually sticks.
So, what does new it actually do?
The new keyword is used to create an object with a function known as a constructor function. Imagine it this way: the function outlines the object's structure, and new brings that object into existence.
function Person(name) {
this.name = name;
}
const person1 = new Person("Aman");
In this example, Person is simply a function. However, when you use new, it transforms into an object creator. Without new, it won't function as expected.
What is a constructor function?
A constructor function is just a normal function used to create multiple similar objects.
We usually write its name starting with a capital letter so it’s easy to recognize.
function Car(brand, model) {
this.brand = brand;
this.model = model;
}
Now you can create multiple objects from it:
const car1 = new Car("Toyota", "Fortuner");
const car2 = new Car("Honda", "City");
Both objects have the same structure, but different values.
What happens when we use new?
When you write:
const car1 = new Car("Toyota", "Fortuner");
JavaScript performs several internal steps. First, it creates a new empty object. Next, it links this object to the constructor's prototype. Then, it executes the function, setting this to the new object. Finally, it returns the object. Imagine it like this:
const car1 = {};
car1.__proto__ = Car.prototype;
Car.call(car1, "Toyota", "Fortuner");
return car1;
This is just to help you understand the flow.
Why prototypes matter here
Every constructor function has something called a prototype.
When you create an object using new, that object is linked to the prototype.
Because of this, the object can use functions defined on the prototype.
function Car(brand, model) {
this.brand = brand;
this.model = model;
}
Car.prototype.showInfo = function () {
console.log(this.brand + " " + this.model);
};
const car1 = new Car("Toyota", "Fortuner");
car1.showInfo();
Even though showInfo is not inside car1, it still works.
That’s because JavaScript looks for it in the prototype.
What are instances?
Objects created using constructor functions are called instances.
function Student(name) {
this.name = name;
}
const student1 = new Student("Ravi");
student1 is an instance of Student.
You can check this:
console.log(student1 instanceof Student); // true
One simple example to remember
function Pen(color) {
this.color = color;
}
const pen1 = new Pen("Blue");
Here's a straightforward explanation: A new object was created, a property was added, it was linked to the prototype, and then returned. That's the entire concept.
Conclusion
The new keyword is what turns a simple function into something that can create objects.
It handles everything for you — creating the object, linking it properly, running the function, and returning the result.
Once you understand this flow, concepts like constructors and prototypes stop feeling confusing and start making sense naturally.




