Skip to main content

Command Palette

Search for a command to run...

The new Keyword in JavaScript

Updated
•5 min read•View as Markdown
The new Keyword in JavaScript

JavaScript is often described as a prototype-based language, but many developers first encounter object creation through constructor functions and the new keyword. Understanding how new works is important because it explains how JavaScript creates object instances, connects prototypes, and enables reusable object behavior.

At first glance, new may seem like a simple keyword that creates objects automatically. In reality, it performs several internal steps behind the scenes. Once you understand those steps, constructor functions and prototypes become much easier to reason about.

What the new Keyword Does

The new keyword is used when calling a constructor function. It tells JavaScript to create a brand-new object based on that constructor.

Example:

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

const user1 = new User("Pritam");

console.log(user1.name);

Output:

Pritam

Here, new User("Pritam") creates a new object and assigns "Pritam" to the name property. Without new, the function behaves like a normal function call.

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

const user1 = User("Pritam");

console.log(user1);

In strict mode, this can throw an error because this becomes undefined. In non-strict mode, it may accidentally attach properties to the global object, which is dangerous and unpredictable.

The new keyword prevents that problem by creating a proper instance object automatically.

Constructor Functions

A constructor function is simply a regular function intended to create objects. By convention, constructor function names start with a capital letter.

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

Using the constructor:

const car1 = new Car("Toyota", "Corolla");
const car2 = new Car("Honda", "Civic");

console.log(car1.brand);
console.log(car2.model);

Output:

Toyota
Civic

Each object gets its own separate data. Constructor functions became common before ES6 classes existed. Even today, classes internally rely on the same prototype system and object creation behavior.

Object Creation Process

When JavaScript encounters the new keyword, it performs several internal operations automatically.

Step 1: Create an Empty Object

When this code runs:

const person = new Person("Alex");

JavaScript first creates an empty object internally.

Conceptually:

const obj = {};

At this point, the object exists but has no properties.

JavaScript then connects the object to the constructor’s prototype.

Conceptually:

obj.__proto__ = Person.prototype;

This step is critical because it enables prototype inheritance.

Step 3: Bind this

Inside the constructor function, this now refers to the newly created object.

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

Equivalent idea:

obj.name = name;

Step 4: Execute the Constructor

The constructor function runs normally.

Person.call(obj, "Alex");

Any properties or methods assigned using this are added to the object.

Step 5: Return the Object

Finally, JavaScript returns the newly created object automatically.

return obj;

So:

const person = new Person("Alex");

ends up producing a fully initialized object.

One of the most important roles of new is prototype linking. Every constructor function automatically has a prototype object.

Example:

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

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

Now create an instance:

const dog = new Animal("Dog");

dog.sound();

Output:

Some sound

Even though sound() is not directly inside dog, JavaScript finds it through the prototype chain.

The lookup process works like this:

  1. Check if dog has sound

  2. If not found, check Animal.prototype

  3. Execute the method if found

This mechanism allows multiple objects to share the same methods instead of duplicating them.

Why Prototype Linking Matters

Imagine creating methods directly inside the constructor:

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

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

Every instance gets its own separate copy of greet.

const u1 = new User("A");
const u2 = new User("B");

This wastes memory because identical functions are recreated repeatedly.

A better approach:

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

User.prototype.greet = function () {
  console.log("Hello");
};

Now all instances share one method through the prototype chain. This is more memory-efficient and scales better.

Instances Created from Constructors

Objects created using new are called instances.

function Laptop(brand) {
  this.brand = brand;
}

const l1 = new Laptop("Dell");
const l2 = new Laptop("Lenovo");

Both l1 and l2 are instances of Laptop. JavaScript provides the instanceof operator to verify this relationship.

console.log(l1 instanceof Laptop);

Output:

true

This works because new linked the object to Laptop.prototype.

Conclusion

The new keyword is much more than a shortcut for creating objects. It controls the full object instantiation process in JavaScript.

When used with constructor functions, new:

  • Creates a fresh object

  • Connects it to a prototype

  • Binds this

  • Executes initialization code

  • Returns the instance

This process forms the foundation of JavaScript’s object-oriented behavior.

Understanding how new works also makes prototypes, inheritance, constructor functions, and ES6 classes easier to understand. Instead of memorizing syntax, you begin to see the underlying mechanics that power object creation in JavaScript.

More from this blog