JavaScript new Keyword

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

While creating JavaScript objects, we use the new keyword, which creates a unique instance of the prototype for us. What if we do not use the new keyword and directly create objects? This article explains in detail the role of the new keyword in creating instances.

Syntax

Parameters

  1. className: The new keyword is followed by the name of the class/prototype for which we want to create an instance.
  2. ListOfArguments: Inside the parenthesis, we pass the list of arguments to the constructor function defined inside the prototype.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example

Description

  1. The new keyword first creates a new empty object.
  2. Then, it binds the newly created object to the prototype of the constructing function.
  3. The new keyword in JavaScript takes care of the this keyword. We know that whenever there is a regular call of a function, the this keyword always points towards the global scope to find its value. Using the new keyword shifts the focus from global scope to the inside of this newly created object, giving us the correct output. The screenshot below demonstrates the same new keywords Therefore, the new keyword in JavaScript binds the data variables and data functions defined inside the constructing function.
  4. Finally, it returns the newly created object.

newly created object

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

1. Object Type and Object Instance

Suppose you want to create an object type for cars. You want this type of object to be called Car, and you want it to have properties for make, model, and year. To do this, you would write the following function:

Now you can create an object called myBike as follows:

2. Object Property That is Itself Another Object

Suppose you define an object called Boy as follows:

And then instantiate two new Boys objects as follows:

Then you can rewrite the definition of Car to include an owner property that takes a Person object, as follows:

To instantiate the new objects, you then use the following:

3. Using 'new' with Classes

Browser Compatibility

Most modern web browsers, like Chrome, Edge, Firefox, Internet Explorer, etc., fully support the new keyword in JavaScript. The complete table of browser compatibility can be seen below.

Browser compatibility

Conclusion

  1. The new keyword is used to create a new and unique instance whenever it is used.
  2. It binds this to the newly created instance, which by default looks in the global window for its value and gives wrong and unexpected outputs.
  3. new binds Data Variables, Data Functions, and [[Prototype]] functions to the object created.
  4. Every object instance created has its own copy of these three things. Hence, variables and functions can be easily accessed using the (.) operator.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more