Understanding the new Keyword in JavaScript
In JavaScript, objects can be created in many ways. One of the most important and commonly used approaches involves the new keyword.
To truly understand how JavaScript creates objects, it is essential to understand how new works internally and how it connects with constructor functions and prototypes.
In this article, we will cover:
What the
newkeyword doesConstructor functions
The object creation process step by step
How
newlinks prototypesInstances created from constructors
What Does the new Keyword Do?
The new keyword is used to create a new object from a constructor function.
Example:
function Person(name) {
this.name = name;
}
const user = new Person("Dipan");
Here, new Person("Dipan") creates a new object and assigns the value "Dipan" to its name property.
Constructor Functions
A constructor function is a regular function used to create multiple objects with similar structure.
By convention, constructor function names start with a capital letter.
Example:
function Car(brand, model) {
this.brand = brand;
this.model = model;
}
const car1 = new Car("Toyota", "Corolla");
const car2 = new Car("Honda", "Civic");
Each time we use new, a new object instance is created.
Object Creation Process (Step by Step)
When you use the new keyword, JavaScript performs several steps internally.
Let us break it down:
const user = new Person("Dipan");
Step 1: Create an Empty Object
JavaScript creates a new empty object:
{}
Step 2: Link the Object to the Prototype
The new object is linked to the constructor’s prototype:
object.__proto__ = Person.prototype
This allows the object to access shared methods.
Step 3: Bind this to the New Object
Inside the constructor function, this now refers to the newly created object.
this.name = name;
Step 4: Execute the Constructor Function
The constructor function runs and assigns values to the object.
Step 5: Return the Object
Finally, the newly created object is returned.
How new Links Prototypes
Every constructor function has a prototype object.
When an object is created using new, it gets access to that prototype.
Example:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name);
};
const user = new Person("Dipan");
user.sayHello();
Explanation:
sayHellois not inside the object itselfIt exists on
Person.prototypeThe object can still access it because of the prototype link
Instances Created from Constructors
Objects created using a constructor are called instances.
Example:
function Animal(type) {
this.type = type;
}
const dog = new Animal("Dog");
const cat = new Animal("Cat");
Here:
dogandcatare instances ofAnimal
You can verify this using instanceof:
console.log(dog instanceof Animal); // true
Each instance:
Has its own properties
Shares methods through the prototype
Relationship Between Constructor and Object
Let us summarize the relationship:
function Person(name) {
this.name = name;
}
const user = new Person("Dipan");
Person→ constructor functionuser→ object instanceuser.__proto__→Person.prototype
This connection allows multiple objects to share behavior efficiently.
Conclusion
The new keyword plays a crucial role in JavaScript object creation. It works together with constructor functions and prototypes to create structured and reusable objects.
Key takeaways:
newcreates a new object from a constructor functionIt sets up the prototype chain automatically
It binds
thisto the new objectConstructor functions help create multiple similar objects
Instances share methods through prototypes
Understanding how new works internally gives you deeper insight into JavaScript’s object system and prepares you for advanced concepts like classes and inheritance.
