Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Published
6 min read
The new Keyword in JavaScript

At some point while learning JavaScript, you've probably written something like this without fully questioning it :

let today = new Date();
let pattern = new RegExp("hello");

The new keyword just... worked. The object appeared. You moved on.

But have you ever stopped to ask, what is new actually doing here? Why does this syntax exist? What happens between typing new Date() and getting a fully formed date object back?

Let's understand this step by step.

1. What is new Keyword ?

The new Keyword in JavaScript is a simple but powerful feature that helps you create objects from functions. If you're just starting with JavaScript, understanding this keyword will make object-oriented programming much easier.

In basic terms, the new keyword allows you to create multiple objects using a single blueprint, called a constructor function. This helps keep your code clean, reusable, and easy to manage.

Importance of new

The new keyword:

  • Helps create objects easily

  • Allows reuse of code

  • Connects objects to shared methods

Where It is Commonly Used

You’ll see new used in:

  • Object creation

  • Built-in objects like Date, Array

  • Custom constructor functions

2. What the new Keyword Does

The new keyword creates a new object and connects it to a constructor function.

Internally new is a set of instructions disguised as a single word.

When you write new SomeFunction(), JavaScript doesn't just call that function. It does four specific things behind the scenes, in order:

  1. Creates a brand new, empty object : {}

  2. Links that object to a prototype

  3. Runs the function with this pointing to that new object

  4. Returns the new object automatically

That's it. Four steps. Every time you use new, these four things happen, whether you're using a built-in like Date, or a constructor you wrote yourself.

Simple Example :

function Person(name) {
  this.name = name;
}

let user = new Person("John");
console.log(user.name);

Output Explanation :

  • A new object is created

  • this refers to that object

  • The property name is added

3. Constructor Functions

A constructor function is just a regular JavaScript function with one convention: its name starts with a capital letter.

That capital letter is a signal that this function is meant to be called with new, not on its own.

Here's the simplest possible constructor :

function Person(name, age) {
  this.name = name;
  this.age = age;
}

// let's use this :

let rahul = new Person("Rahul", 28);
let priya = new Person("Priya", 24);

console.log(rahul.name);  // "Rahul"
console.log(priya.age);   // 24

That's it. No return statement. No magic. Just properties being attached to this.

Two separate objects, created from the same template. Each has its own name and age. They're independent, changing one doesn't affect the other.

what happens if you call Person without new :

let whoops = Person("Meera", 22);

console.log(whoops);        // undefined  — no object returned
console.log(window.name);   // "Meera"   — this.name went to the global object!

Without new, the function runs normally. this points to the global object (or is undefined in strict mode).

4. Object Creation Process ( Step by Step )

This is where The new Keyword in JavaScript becomes really interesting.

Let's walk through exactly what happens when you write new Person("Rahul", 28), one step at a time.

Step 1 : A new empty object is created

JavaScript internally creates this:

let obj = {};

Nothing on it yet. A blank slate.

Step 2 : The prototype is linked

JavaScript connects obj to Person.prototype, a special object that holds shared behaviors.

obj.__proto__ = Person.prototype;

Step 3 — The constructor runs with this = obj

Now Person is called, but with this pointing to our new empty object:

// Inside Person, this = obj
this.name = "Rahul";   // obj.name = "Rahul"
this.age = 28;         // obj.age = 28

Properties are being stamped onto the fresh object.

Step 4 : The object is returned automatically

You didn't write a return statement, and that's correct. new handles the return automatically:

return obj;  // JavaScript does this for you

The finished object lands in rahul.

Every function in JavaScript has a property called prototype. It's an object that sits quietly attached to the function, waiting to be used.

When you create an object with new, that object gets a hidden link to the constructor's prototype and share methods via the prototype. This means the object can access anything on prototype, even though those properties aren't directly on the object itself.

Example with Prototype

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  return this.name + " makes a sound";
};

let dog = new Animal("Dog");
console.log(dog.speak());

This avoids duplicating methods for every object.

6. Instances Created From Constructors

Every object created with new is called an instance of that constructor.

The word "instance" just means: a specific object built from a specific template. There may be multiple objects created from one constructor, each object is called an instance of that particular constructor.

rahul is an instance of Person. laptop is an instance of Product.

Each object is separate but shares the same structure.

let user1 = new Person("Alice");
let user2 = new Person("Bob");

Both objects:

  • Have different values

  • Share the same methods

7. A Note on Modern Syntax : Classes

You may have seen this style in modern JavaScript:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log(`Hi, I'm ${this.name}!`);
  }
}

let rahul = new Person("Rahul", 28);
rahul.greet();  // "Hi, I'm Rahul!"

This class syntax was introduced in ES6. It looks different, cleaner, more familiar if you've used other languages, but under the hood, it does exactly the same thing as the constructor function approach you just learned.

class is syntactic sugar. The prototype chain, the new keyword, the four-step creation process, all of it still happens. JavaScript didn't change how objects work. It just gave us a different way to write it.

If you understand constructor functions and new, you already understand classes.

Constructor Function Style Class Style
function Person(name) { this.name = name; } constructor(name) { this.name = name; }
Person.prototype.greet = function() {} greet() {} inside the class body
new Person("Rahul") new Person("Rahul") — identical

Classes are the preferred modern style — but the mental model is identical. Knowing both makes you a developer who truly understands the language, not just its surface.