# The new Keyword in JavaScript


In JavaScript, objects can be created in multiple ways.

One powerful way is to use the:

```javascript
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:

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

const user1 = new Person("Rahul");

console.log(user1);
```

Output:

```javascript
{ 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:

```javascript
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:

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

const user = Person("Rahul");

console.log(user);
```

This does not work properly because:

```id="jlwm06"
No new object was created
```

The `new` keyword is what makes constructor functions behave correctly.

* * *

## Creating Objects Using `new`

Example:

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

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

console.log(user1);
```

Output:

```javascript
{
  name: "Rahul",
  age: 22
}
```

* * *

## Step-by-Step Object Creation Process

When JavaScript sees:

```javascript
new Person("Rahul")
```

it performs several steps automatically.

* * *

## Step 1: Create a New Empty Object

JavaScript internally creates:

```javascript
{}
```

* * *

## Step 2: Link the Object to Prototype

The object is connected to:

```javascript
Person.prototype
```

This allows the object to access shared methods.

* * *

## Step 3: `this` Points to the New Object

Inside the constructor:

```javascript
this.name = name;
```

`this` refers to the newly created object.

* * *

## Step 4: Return the Object

JavaScript automatically returns the new object.

Final result:

```javascript
{
  name: "Rahul"
}
```

* * *

## Object Creation Flow

```id="jlwm14"
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:

```id="jlwm15"
Instances
```

Example:

```javascript
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

```id="jlwm17"
Constructor Function
        ↓
Creates
        ↓
Object Instances
```

Example:

```id="jlwm18"
Student()
   ↓
s1
s2
s3
```

One constructor can create many objects.

* * *

## Adding Methods to Constructor Functions

Example:

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

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

const user = new Person("Rahul");

user.greet();
```

Output:

```id="jlwm20"
Hello Rahul
```

The created object now has properties and methods.

* * *

## How `new` Links Prototypes

Every constructor function has a:

```javascript
prototype
```

property.

Objects created with `new` are linked to that prototype.

Example:

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

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

const user = new Person("Rahul");

user.sayHello();
```

Output:

```id="jlwm23"
Hello
```

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

* * *

## Prototype Linking Visualization

```id="jlwm24"
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:

```javascript
this.greet = function(){}
```

every object gets its own copy.

But with prototypes:

```javascript
Person.prototype.greet = function(){}
```

all objects share the same method.

This improves memory efficiency.

* * *

## Real-World Example

Imagine a blueprint for cars.

```id="jlwm27"
Car Blueprint
     ↓
Creates
     ↓
Many Car Objects
```

Each car:

*   has its own data
    
*   shares common behavior
    

Constructor functions work similarly.

* * *

## Checking Instances

JavaScript provides:

```javascript
instanceof
```

Example:

```javascript
console.log(user instanceof Person);
```

Output:

```id="jlwm30"
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:

```id="jlwm31"
Book
```

Add:

*   title
    
*   author
    

* * *

## 2\. Create Multiple Instances

Example:

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

* * *

## 3\. Add a Prototype Method

Add:

```javascript
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:

```id="jlwm34"
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!
