Skip to main content

Command Palette

Search for a command to run...

Understanding the new Keyword in JavaScript

Updated
•3 min read•View as Markdown

When you see the new keyword in JavaScript, it usually appears with functions.

At first, it feels confusing.

But the idea is simple:

new is used to create objects from a function.

It takes a function and turns it into a blueprint for creating objects.


What the new Keyword Does

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

Example:

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

let user1 = new Person("Ishan", 21)

console.log(user1)

Here:

  • Person is a constructor function

  • new Person() creates a new object


Constructor Functions

A constructor function is just a normal function, but it is used to create objects.

By convention, constructor names start with a capital letter.

function Car(brand) {
  this.brand = brand
}

let car1 = new Car("Toyota")

The this keyword refers to the new object being created.


https://images.openai.com/static-rsc-4/l3QtKRjobzB-trDDVXsftH6KDT427G6dux5CZTeKzYR4pfNY_uLuSanH9wPy2I-YeGl9VjiBpKJcGfRwGBerQEeUu736o8ZmIbvXuTlMq0a6Ys8mUGGzl-x0IDix4hPFU-x0LLreO_g-1svcHYnGlQxD-AcAZRfNFbnggtvzAbGGijdCLNCT1EAGCDfSCW30?purpose=fullsize

Object Creation Process (Step by Step)

When you use new, JavaScript does multiple things behind the scenes.

Example:

let user = new Person("Ishan", 21)

What actually happens:

  1. A new empty object is created

  2. this is set to that object

  3. Properties are assigned (name, age)

  4. The object is returned

So new is not just creating — it’s doing a full process.


Every object in JavaScript has a prototype.

When you use new, the created object is linked to the constructor’s prototype.

Example:

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

Animal.prototype.sound = function () {
  console.log("Some sound")
}

let dog = new Animal("Dog")

dog.sound()

Here:

  • dog gets access to sound()

  • not because it’s inside the object

  • but because of prototype linking


https://images.openai.com/static-rsc-4/LA3mlFrJAexYiqqKCdigld2Clk5Z7yENT78uVc12cbSyCcHh5jGlt9YT8oNOW0yz-Dn41UCW94KX-O1OEwm2fBOETevKY0M0L9r_LvIq3IX3hKbCBcwuZlQTMO9_QZoqHfDjJGbutZYmkYKQNNJ7J1XWpe-VY4j32CQkfnkjMLSkor0KVEIMuIbulm2MLqzz?purpose=fullsize

Instances Created from Constructors

Every time you use new, a new instance is created.

let user1 = new Person("A", 20)
let user2 = new Person("B", 25)

Each object:

  • has its own data

  • shares common behavior (via prototype)

So constructors help create multiple objects easily.


How to Think About It

Instead of thinking about syntax, think like this:

  • constructor = blueprint

  • new = creates object

  • object = instance


One Line Summary

new creates an object from a function and sets up its structure.


Conclusion

The new keyword is used to create objects from constructor functions.

It not only creates the object but also sets up this, assigns values, and links prototypes.

Understanding this helps you see how JavaScript builds objects behind the scenes.