# The new Keyword — How JavaScript Builds Objects

What Does the new Keyword Do?

When you write `new` before a function call, you are telling JavaScript: "Don't just run this function use it as a *blueprint* to create a brand new object."

Without `new`, a constructor function behaves like any other function. *With* `new`, JavaScript quietly does four things behind the scenes that turn that function into an object factory.

**A useful mental model:** Think of a constructor function as a cookie cutter, and `new` as the act of pressing it into dough. Each press produces a separate, independent cookie — the same shape, but its own thing

**Here is the simplest possible demonstration of what** `new` **gives you:**

```javascript
function Car() {
  this.color = "red";
}

// WITHOUT new — just a normal function call
Car(); // returns undefined, this = window

// WITH new — creates a fresh object
const myCar = new Car();
console.log(myCar.color); // "red"
```

**Constructor Functions**

A constructor function is just a regular JavaScript function, but written with a specific purpose: to describe what every object created from it should look like.

By convention, constructor functions are named with a **capital letter** like `Person`, `Car`, or `Product`. This is not required by JavaScript, but it is a universally respected signal that says "call me with `new`."

Inside the constructor, `this` refers to the new object being created. Every property you attach to `this` becomes a property of that new object.

```javascript
// Constructor function — the blueprint
function Person(name, age) {
  this.name = name; // each object gets its own name
  this.age = age;
  this.greet = function() {
    console.log("Hi, I'm " + this.name);
  };
}

// Creating objects (instances) from the blueprint
const alice = new Person("Alice", 30);
const bob = new Person("Bob", 25);

alice.greet(); // "Hi, I'm Alice"
bob.greet(); // "Hi, I'm Bob"
```

**Notice:** `alice` and `bob` are completely separate objects. Changing `alice.age` has zero effect on `bob.age`. The constructor described the *shape*, but each instance owns its own data.

**1.The Object Creation Process — Step by Step**

When you write `new Person("Alice", 30)`, JavaScript silently performs four steps in order. Understanding these steps removes all the mystery from `new`.

**2\. A brand new empty object is created**

JavaScript creates `{}` in memory. This is the object that will eventually be returned to you.

**3.The prototype is linked**

The new object's internal `[[Prototype]]` is connected to `Person.prototype`. This gives the object access to shared methods. (More on this in section 4.)

**4.The constructor runs with this = new object**

The function body executes. Every `this.something = ...` line adds a property to the new object created in step 1.

**5.The new object is returned automatically**

Unless you explicitly `return` a different object, JavaScript hands back the newly created object. You don't need to write `return this`.

![](https://cdn.hashnode.com/uploads/covers/644510985259d501cf938e64/ad98234c-acb7-40a8-9eaa-81d868393c8e.png align="center")

How new Links Prototypes

This is where things get really interesting. When JavaScript creates a new object with `new`, it doesn't just fill in properties — it also connects the object to a *shared pool of methods* called the **prototype**.

Every function in JavaScript automatically has a property called `.prototype`. When you add a method to `Person.prototype`, *every* object created by `new Person()` can use that method — without each object storing its own copy of it. This saves memory and keeps things organized.

```javascript
function Person(name, age) {
  this.name = name; // stored ON the object (own property)
  this.age = age;
}

// Shared method — stored on the prototype, not each object
Person.prototype.greet = function() {
  console.log("Hi, I'm " + this.name);
};

const alice = new Person("Alice", 30);
const bob = new Person("Bob", 25);

alice.greet(); // works! looks up prototype chain
bob.greet(); // also works! same shared method

// greet lives on the prototype, not on alice or bob
console.log(alice.hasOwnProperty("name")); // true
console.log(alice.hasOwnProperty("greet")); // false
```

![](https://cdn.hashnode.com/uploads/covers/644510985259d501cf938e64/8f85bdb1-e63f-4763-8a84-0e1a73f97890.png align="center")

**How lookup works:** When you call `alice.greet()`, JavaScript first looks on `alice` itself. It's not there. So it follows the prototype link up to `Person.prototype`, finds `greet` there, and calls it. This is called the *prototype chain*.

**Instances Created from Constructors**

Every object you create with `new` is called an **instance** of that constructor. Each instance is independent — its own properties, its own data — but they all share the same prototype methods.

You can confirm an object's origin using `instanceof`, which checks whether the object was created from a given constructor:

```javascript
function Person(name, age) {
  this.name = name;
  this.age = age;
}
Person.prototype.greet = function() {
  console.log("Hi, I'm " + this.name);
};

const alice = new Person("Alice", 30);
const bob = new Person("Bob", 25);
const carol = new Person("Carol", 28);

// Each is its own independent object
alice.age = 31; // only changes alice
console.log(bob.age); // still 25 — unaffected

// instanceof confirms their origin
console.log(alice instanceof Person); // true
console.log(alice instanceof Object); // also true!
```

**Here's a side-by-side look at two instances in memory — same shape, completely separate data:**

**Note:** Modern JavaScript introduced the `class` keyword, which looks cleaner but does the exact same thing underneath — it still uses constructor functions and prototype linking. Understanding `new` at this level means you truly understand how JavaScript classes work too.

**Quick recap:** `new` triggers four silent steps — create an empty object, link it to the constructor's prototype, run the function with `this` pointing to that object, then return it. Constructor functions are blueprints named with a capital letter. Each call to `new` produces an independent *instance* with its own data, but all instances share methods via the prototype chain — keeping memory lean and behavior centralized.
