Skip to main content

Command Palette

Search for a command to run...

Understanding the new Keyword in JavaScript

Learn how JavaScript creates objects step by step using constructor functions

Updated
3 min read
Understanding the new Keyword in JavaScript

When you first see the new keyword in JavaScript, it might feel confusing. It looks small, but it does something very powerful — it helps you create objects using a pattern.

Let’s break it down slowly, like we are learning it together.


What does the new keyword do?

In simple words, the new keyword is used to create a new object from a constructor function.

  • A constructor function is like a blueprint

  • The new keyword is the tool that builds an object using that blueprint


Constructor Functions (The Blueprint)

Before understanding new, you need to understand constructor functions.

A constructor function is just a normal function, but by convention:

  • Its name starts with a capital letter

  • It is used to create objects

Example:

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

This function doesn't return anything explicitly, but it is designed to create objects.


Using new with a constructor

Now lets use new:

const user1 = new Person('Kousik',20)

That single line does a lot behind the scenes.


What actually happens when you use new ?

This is the most important part. When you write:

const user1 = new Person('Kousik',20)

JavaScript does these steps internally:

  • A new empty object is created
{}
  • this is linked to that object

Inside the constructor:

this.name = name
this.age = age

Now becomes:

{
    name: 'Kousik',
    age 23
}
  • The object is linked to the constructor's prototypewjdhd

JavaScript connects the object to:

Person.prototype

So the object can access shared methods later.


  • The object is returned automatically

Even though you did not write return, JavaScript returns the object.

So:

const user1 = new Person('Kousik',20)

Becomes:

user1 = { 
    name: 'Kousik',
    age 20
}

How new connects to prototypes

Every constructor function has a property called prototype.

You can add methods there:

Person.prototype.greet = function() {
    console.log('Hello, my name is ' + this.name)
}

Now every object created with new Person() can use this method:

user1.greet()    // Hello, my name is Kousik

Important:

  • The method is not copied into every object

  • It is shared via the prototype

This saves memory and keeps code clean


Instances created from constructors

When you create an object using new, that object is called an instance of the constructor.

Example:

const user1 = new Person('Kousik, 20)
const user2 = new Person('Simran',20)

Here:

  • user1 is an instance of Person

  • user2 is also an instance of Person

You can check like:

console.log(user1 instanceof Person)

Why use new?

Without new, things break:

const user1 = Person('Kousik', 20)

Now:

  • No new object is created

  • this may point to global object (or undefined in strict mode)

  • You won’t get the expected result

So always use new with constructor functions.

Conclusion

The new keyword helps you create objects from constructor functions in a clean and structured way. It connects the object to its constructor and prototype automatically. Once you get comfortable with it, working with objects in JavaScript becomes much easier.