The new Keyword in JavaScript

Have you ever seen code like this and wondered what on earth new does?
const myCar = new Car("Tesla", "red");
You're not alone. Most tutorials throw around words like "constructor," "prototype," "instance" and suddenly your brain feels like a spinning beach ball. Let's fix that right now with one simple story.
🏭Imagine a Cookie Cutter Factory
Picture a cookie cutter in the shape of a star. Every time you press it into dough, you get a new star-shaped cookie. The cutter itself is not a cookie — it's just the mould that knows what shape to make.
🔧Writing a Constructor Function
A constructor function is just a regular function that we use as a blueprint. By convention, we capitalize the first letter to signal "hey, this is a constructor!"
Let's build a Robot factory:
// 🤖 This is our "cookie cutter" — the blueprint
function Robot(name, color) {
this.name = name; // every robot gets a name
this.color = color; // every robot gets a color
this.isAlive = true; // every robot starts alive!
}
// 🏭 Using "new" to stamp out real robots
const r1 = new Robot("Robo-1", "blue");
const r2 = new Robot("Robo-2", "red");
console.log(r1.name); // "Robo-1"
console.log(r2.color); // "red"
💡 What is this? Inside a constructor, this refers to the brand new object being created. Think of it as the robot pointing at itself saying "My name is…". Every time new Robot() runs, a fresh this is born.
🪄What new Does : Step by Step
Here's the magic trick. When you write new Robot("Robo-1", "blue"), JavaScript secretly does four things for you behind the scenes:
Creates a brand new empty object {}: JavaScript silently creates a blank object. Like opening a fresh empty box before you put toys in it.
Points this at that new object Inside your constructor, this now means that fresh empty object. So this.name = name puts the name inside that object.
Links the object to the constructor's prototype The new object gets a secret back-channel to Robot.prototype. This is how all robots can share methods without duplicating code. (More on this in a moment!)
Returns the new object automatically You don't need to write return — new hands back the finished object for free. That's what gets stored in r1 or r2.
🧬How Prototypes Work — The Instruction Manual
Here's a fun way to think about it. When you buy a toy robot, it comes with an instruction manual. The manual isn't inside the robot — it lives in the box. But every copy of that robot refers to the same manual.
In JavaScript, prototype is that shared instruction manual. Methods (actions the object can do) live on the prototype so every instance can use them without each one carrying its own copy.
// Add a shared method to the instruction manual (prototype)
Robot.prototype.greet = function() {
return `Hi! I am \({this.name} and I am \){this.color}.`;
};
// Both robots can now use .greet() 🎉
console.log(r1.greet()); // "Hi! I am Robo-1 and I am blue."
console.log(r2.greet()); // "Hi! I am Robo-2 and I am red."
// The greet function lives only once — on the prototype
console.log(r1.greet === r2.greet); // true ✅
🌿 Why does this matter? If you had 1,000 robots and each carried its own copy of greet(), that's 1,000 copies of the same function wasting memory. With prototypes, there's just one copy that every robot points to. Efficient and elegant.
🔍Checking if Something is an Instance
JavaScript gives you a way to ask "was this object made from that constructor?" using the instanceof operator:
console.log(r1 instanceof Robot); // true ✅
console.log(r1 instanceof Array); // false ❌
// You can also check the constructor directly
console.log(r1.constructor === Robot); // true ✅
✅ ⚠️What Happens If You Forget new?
This is a classic mistake. If you call a constructor without new, this won't point to a fresh object — it'll point to the global object (or be undefined in strict mode), and everything breaks:
const oops = Robot("BadBot", "green"); // Missing "new"!
console.log(oops); // undefined 😱
// "name" and "color" leaked onto the global scope — messy!
🛡️ Modern Solution Modern JavaScript uses class syntax, which enforces new and throws a clear error if you forget it. Under the hood, class is just constructor functions + prototypes with nicer syntax — same concept, cleaner code.
✨The Modern Class Syntax
Everything we built above can be written with the class keyword, which most developers prefer today. It does exactly the same thing — just reads nicer:
class Robot {
constructor(name, color) {
this.name = name;
this.color = color;
this.isAlive = true;
}
greet() {
return `Hi! I am \({this.name} and I am \){this.color}.`;
}
}
const r3 = new Robot("Robo-3", "gold");
console.log(r3.greet()); // "Hi! I am Robo-3 and I am gold."
Same new, same prototype linking — just wrapped in a much friendlier package.
🧠 The 30-Second Summary
A constructor function is a blueprint (like a cookie cutter or a toy mould).
The new keyword stamps out a fresh object from that blueprint, points this at it, links it to the prototype, and returns it automatically.
The prototype is a shared instruction manual — methods live there so all instances can share them without duplication.
Each result of new SomeThing() is an instance — unique data, shared behaviour.




