The new Keyword in JavaScript
What Does new Do?
The new keyword creates an object from a constructor function.
It automates object creation and prototype linking.
Constructor Functions
A constructor function is a normal function used to create objects.
By convention, constructor names start with uppercase letters.
Example
function User(name, age) {
this.name = name;
this.age = age;
}
Creating an Object With new
const user1 = new User("Ali", 22);
console.log(user1);
Output
User { name: 'Ali', age: 22 }
Constructor → Instance Flow
flowchart LR
A[User Constructor] --> B[new User()]
B --> C[user1 instance]
What Happens Internally?
When using new:
A new empty object is created
this points to that object
Properties are added
Prototype is linked
Object is returned automatically
Step-by-Step Object Creation
Step 1: Empty Object Created
{}
Step 2: this Points to Object
this = {}
Step 3: Properties Added
this.name = name; this.age = age;
Step 4: Object Returned
Final object:
{
name: "Ali",
age: 22
}
Without new
Incorrect:
function User(name) {
this.name = name;
}
const user = User("Ali");
console.log(user);
Problem
undefined
Because no object was created automatically.
Adding Methods to Constructor
function User(name) {
this.name = name;
this.greet = function () {
console.log(`Hello ${this.name}`);
};
}
const user1 = new User("Sara");
user1.greet();
Output
Hello Sara
Instances Created From Constructors
Each object created with new is called an instance.
Example
const user1 = new User("Ali");
const user2 = new User("Sara");
Both are instances of User.
Checking Instance Type
Use:
instanceof
Example
console.log(user1 instanceof User);
Output
true
Prototype Linking
Every object created with new is linked to the constructor’s prototype.
Example
function User(name) {
this.name = name;
}
User.prototype.greet = function () {
console.log(`Hello ${this.name}`);
};
const user1 = new User("Ali");
user1.greet();
Why Use Prototype?
Methods are shared between instances instead of recreated every time.
Better memory efficiency.
Prototype Linking Visual
Constructor vs Instance
Real-World Analogy
Multiple Instances Example
function Product(name, price) {
this.name = name;
this.price = price;
}
const product1 = new Product("Laptop", 50000);
const product2 = new Product("Phone", 25000);
console.log(product1);
console.log(product2);
Output
Product { name: 'Laptop', price: 50000 }
Product { name: 'Phone', price: 25000 }
Common Beginner Mistake
Forgetting new.
Incorrect:
const user = User("Ali");
Correct:
const user = new User("Ali");
Modern Alternative: Classes
Modern JavaScript often uses classes internally built on constructors.
Example
class User {
constructor(name) {
this.name = name;
}
}
const user1 = new User("Ali");
Key Takeaways
Final Notes
The new keyword is a foundational JavaScript concept because it powers:
Constructor functions
Classes
Prototype inheritance
Object-oriented programming
Even modern class syntax internally uses constructor and prototype behavior.
0 Comments
Sign in to join the conversation
No comments yet. Be the first to comment!