In this article, we will understand exactly what the new keyword is, and why we use the new keyword, and how we can use the new keyword.
These are the following features to use the new keyword:
- The new keyword creates a new empty object, with a type of object.
- The new keyword sets the internal prototype property of the constructing function.
- The new keyword binds this variable to the newly created object.
- The new keyword returns the new object.
Example 1: In the below example, we will create a message function to print names with greetings and we will create an instance of message function.
Output: Output on console
Example 2: In the below example, we will create a student function and create an instance of the function using the new keyword as shown below.