In JavaScript, the new
keyword is used to create an instance of a constructor function or a class, and it helps set up an object with the properties and methods defined within that constructor. It essentially creates a new object and applies the constructor function to it.
Key Uses of the new
Keyword
- Creating Instances of Constructor Functions: When you use the
new
keyword with a constructor function, it creates a new object, sets the context (this
) inside the constructor to refer to that object, and initializes the object’s properties and methods.Example: Constructor Function
// Constructor function function Person(name, age) { this.name = name; this.age = age; } // Using 'new' to create an instance of 'Person' const person1 = new Person('Alice', 30); console.log(person1.name); // Output: Alice console.log(person1.age); // Output: 30
In the example above:
new Person('Alice', 30)
creates a new object.- The
this
keyword inside the constructor function refers to the new object, soperson1
gets the propertiesname
andage
.
- Creating Instances of Classes (ES6 Classes): Starting from ES6, JavaScript introduced classes as a more structured way to define constructor functions. You can use the
new
keyword to create an instance of a class.Example: Class
class Person { constructor(name, age) { this.name = name; this.age = age; } } const person1 = new Person('Bob', 25); console.log(person1.name); // Output: Bob console.log(person1.age); // Output: 25
The usage of the
new
keyword in the class works the same as with constructor functions. - Creating Instances of Built-in Objects: The
new
keyword can also be used with built-in objects, such asDate
,Array
,RegExp
, etc., to create instances of those objects.Example: Date Object
const today = new Date(); console.log(today); // Output: Current date and time (e.g., Mon Jan 21 2025 12:34:56 GMT+0000)
Example: Array Object
const myArray = new Array(1, 2, 3, 4); console.log(myArray); // Output: [1, 2, 3, 4]
How new
Works
When you invoke a function with the new
keyword, here’s what happens behind the scenes:
- A new empty object is created.
- The
this
value inside the constructor function is bound to the newly created object. - Properties and methods are assigned to the newly created object through the constructor.
- The new object is returned from the constructor (if the constructor does not explicitly return an object, the new object created by
new
is returned by default).
Example of How new
Works Behind the Scenes:
function Person(name, age) {
this.name = name;
this.age = age;
}
const person1 = new Person('Alice', 30);
// Equivalent to the following without using 'new':
// const person1 = {};
// Person.call(person1, 'Alice', 30);
console.log(person1.name); // Alice
console.log(person1.age); // 30
Here, the new
keyword automatically:
- Creates a new object (
{}
). - Sets
this
to point to that object. - Executes the
Person
function and assigns properties to the object. - Returns the object (if no other value is returned).
Important Notes:
- You cannot use
new
with non-constructor functions like regular functions. Doing so will result in an error. - In ES6 classes, using
new
is mandatory when creating an instance; it cannot be used as a plain function.
Summary
- The
new
keyword is used to create instances of constructor functions or classes. - It creates a new object, binds
this
inside the constructor to that object, and initializes properties and methods. - It is used with both user-defined constructors and built-in objects like
Date
,Array
, etc.
Let me know if you’d like more details or examples!