Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Updated
5 min read
The new Keyword in JavaScript

In JavaScript, objects can be created in multiple ways.

One powerful way is to use the:

new

keyword.

The new keyword is commonly used with:

  • Constructor functions

  • Classes

  • Object creation patterns

Understanding how new Works helps you understand the foundation of object-oriented programming in JavaScript.

In this article, we’ll learn:

  • What the new keyword does

  • Constructor functions

  • The object creation process

  • How new links prototypes

  • Instances created from constructors

Let’s start with the basics.


What Does the new Keyword Do?

The new keyword creates a new object from a constructor function.

Example:

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

const user1 = new Person("Rahul");

console.log(user1);

Output:

{ name: "Rahul" }

Here:

  • new creates a brand-new object

  • The object gets linked to the constructor

  • this points to the new object


What Is a Constructor Function?

A constructor function is a normal function used to create objects.

By convention, constructor names start with a capital letter.

Example:

function Car(brand, model) {
  this.brand = brand;
  this.model = model;
}

This function acts like a blueprint for creating car objects.


Creating Objects Without new

Example:

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

const user = Person("Rahul");

console.log(user);

This does not work properly because:

No new object was created

The new keyword is what makes constructor functions behave correctly.


Creating Objects Using new

Example:

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

const user1 = new Person("Rahul", 22);

console.log(user1);

Output:

{
  name: "Rahul",
  age: 22
}

Step-by-Step Object Creation Process

When JavaScript sees:

new Person("Rahul")

it performs several steps automatically.


Step 1: Create a New Empty Object

JavaScript internally creates:

{}

The object is connected to:

Person.prototype

This allows the object to access shared methods.


Step 3: this Points to the New Object

Inside the constructor:

this.name = name;

this refers to the newly created object.


Step 4: Return the Object

JavaScript automatically returns the new object.

Final result:

{
  name: "Rahul"
}

Object Creation Flow

new Person()
      ↓
Create Empty Object
      ↓
Link Prototype
      ↓
Bind this
      ↓
Return Object

This is what the new keyword does internally.


Instances Created from Constructors

Objects created using constructor functions are called:

Instances

Example:

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

const s1 = new Student("Rahul");
const s2 = new Student("Aman");

Here:

  • s1 is an instance of Student

  • s2 is another instance of Student


Constructor → Instance Relationship

Constructor Function
        ↓
Creates
        ↓
Object Instances

Example:

Student()
   ↓
s1
s2
s3

One constructor can create many objects.


Adding Methods to Constructor Functions

Example:

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

  this.greet = function() {
    console.log("Hello " + this.name);
  };
}

const user = new Person("Rahul");

user.greet();

Output:

Hello Rahul

The created object now has properties and methods.


Every constructor function has a:

prototype

property.

Objects created with new are linked to that prototype.

Example:

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

Person.prototype.sayHello = function() {
  console.log("Hello");
};

const user = new Person("Rahul");

user.sayHello();

Output:

Hello

Even though sayHello() is not directly inside the object, it works because of prototype linking.


Prototype Linking Visualization

user object
    ↓
linked to
    ↓
Person.prototype

This is one of JavaScript’s core concepts.


Why Prototypes Are Useful

If methods are added inside the constructor:

this.greet = function(){}

every object gets its own copy.

But with prototypes:

Person.prototype.greet = function(){}

all objects share the same method.

This improves memory efficiency.


Real-World Example

Imagine a blueprint for cars.

Car Blueprint
     ↓
Creates
     ↓
Many Car Objects

Each car:

  • has its own data

  • shares common behavior

Constructor functions work similarly.


Checking Instances

JavaScript provides:

instanceof

Example:

console.log(user instanceof Person);

Output:

true

This confirms the object was created using that constructor.


Practice Assignment

Try these exercises yourself.


1. Create a Constructor Function

Create a constructor called:

Book

Add:

  • title

  • author


2. Create Multiple Instances

Example:

const b1 = new Book(...);
const b2 = new Book(...);

3. Add a Prototype Method

Add:

Book.prototype.getDetails

and print book information.


4. Use instanceof

Check whether objects belong to the constructor.


Final Thoughts

The new keyword is one of JavaScript’s most important object creation features.

It helps JavaScript:

  • Create new objects

  • Bind this correctly

  • Connect objects to prototypes

  • Build reusable object structures

The key idea is:

Constructor function = blueprint
Objects = instances created from that blueprint

Understanding new and constructor functions builds the foundation for learning:

  • Prototypes

  • Classes

  • Inheritance

  • Object-oriented programming

Mastering these concepts will make advanced JavaScript much easier to understand.


And now, you know what the new keyword in JavaScript is.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!