What is New in JavaScript?3 Mar 2025 | 5 min read What is a New Keyword?In JavaScript, the new keyword is used to create an instance of an object with the use of the constructor method. It means when we create a new instance, JavaScript calls the constructor method every time we create an instance. In simple words, the new keyword helps us to create an instance of the object that has a constructor method. With the use of a new keyword, users can create instances of user-defined as well as built-in object types. We can create instances of classes, prototypes, or constructor methods. In JavaScript, when we create a function with new keyword the function is used as constructor. The new keyword will do the following:
Syntax Parameter Constructor: In JavaScript, it is the name of the constructor method or class name. Argument: In JavaScript, there can be multiple arguments to initialize the object properties with them. Using the 'new' keyword with Function ConstructorIn JavaScript, to use a function as a constructor, we should call the function while a new keyword is placed before the function name. In JavaScript, we can also define multiple objects with the use of a function constructor. The constructor method works as the object's prototype. Syntax: The syntax of the new keyword and constructor function are as follows: Example Explanation In the above example, we defined the Watch() constructor function. The watch() constructor function initializes the brand, price, and type properties. Then, we created two new instances of the Watch() function and printed them in the output. Output: The rolex object is: {"brand":"Rolex","price":40000,"type":"analog"} The sonata object is: {"brand":"sonata","price":3000,"type":"digital"} Using 'new' keyword with ClassIn JavaScript, we can also use the new keyword to define the instance of a class. When we use the new keyword with the class, it provides the blueprint for the object. Before ES6, constructor functions defined object templates; after ES6, classes are used instead. Example Explanation In the above example, we have defined the 'WindowClass class'. In this 'WindowClass' we have added the constructor to initialize the properties. We have also added the getDetails() method in the class. After that, we used the new keyword followed by the class name to define an object of the WindowClass. Output: Color: Red Size: large Type: wooden Using 'new' keyword with a built-in objectWe can also use the new keyword to define the instance of the built-in object having a constructor function. Syntax In the above syntax, we have passed the numeric value as an argument of the Number() constructor. Example Output: num = 10, typeof num object str = Hello, typeof str object The purpose of the new keyword in JavaScriptCreating a new objectIn JavaScript, the new keyword is used to create a new instance of an object. When we call a constructor function with new, it creates the object. Example Setting the prototypeIn JavaScript, the new object's internal [[Prototype]] property is set to the constructor functions prototype property. It allows the new object to inherit the properties and methods from the constructor's prototype. Binding this to the new objectIn JavaScript, inside the constructor function, `this` refers to the new object that is being created. It also allows us to add the properties and methods to the new object. Returning the new objectThe 'new' Keyword in JavaScript implicitly returns the object created by the constructor function. Now, If the constructor function explicitly returns an object, that object will be returned instead. ConclusionIn JavaScript, the new keyword is a fundamental tool for object creation and plays a crucial role in the language's object-oriented capabilities. By invoking a constructor function or class with new, you generate a new object instance with its own properties and methods, adhering to the blueprint defined by the constructor. This process involves setting up the prototype chain, binding this context to the new object, and handling the return of the created object. Next TopicJavaScript Copy Object |
In JavaScript, a fixed collection of named values is represented via enums. They enable programmers to create a list of named constants that may be used across codebase to improve error ention, readability, and maintainability. Enumerations, often known as enums, are used in JavaScript to express a...
13 min read
JavaScript is a versatile and widely used programming language, particularly in web development. Adding two numbers in JavaScript is one of the most fundamental operations you'll encounter when working with this language. In this article, we'll explore various methods for adding two numbers in JavaScript, catering...
3 min read
The console.log() method is an essential method in JavaScript. It is utilized to print the output to the console. Syntax: console.log(""); Parameters: The console.log() method takes a parameter which can be any message, an object, array, etc. Return value: It returns a value which is given...
4 min read
Making a game in JavaScript might be a satisfying encounter that further develops your coding skills while likewise giving a charming, intelligent application to illustrate. The Snake Game is a well-known and exemplary game to build as a beginning venture. This game requires dealing with a...
10 min read
Clear Intervals are fundamental additives of JavaScript that might be used to manage movements that want to be finished often or after a put-off and to manipulate the drift of execution. These capabilities are provided through the setInterval and setTimeout features, which let programmers write code...
9 min read
What is Null in JavaScript? In JavaScript, null is a value that represents the intentional absence of any object value. It is technically a primitive type, although in some cases, it behaves as an object. In other words, null indicates the delibrate absence of any object value. It's...
3 min read
We can create external JavaScript file and embed it in many html page. It provides code re usability because single JavaScript file can be used in several html pages. An external JavaScript file must be saved by .js extension. It is recommended to embed all JavaScript files...
2 min read
What is 1 1 in JavaScript? In JavaScript, 1 + "1" is an expression in which the first operand is 1 and the second operand is the string "1". Now you can see the second operand is a string, JavaScript will perform the string concatenation and...
3 min read
At first, coding can seem difficult especially for beginners. This article aims to assist you in simplifying matters. This article will guide the process of “commenting out multiple lines in JavaScript”. Mastering the skill of commenting on your code is crucial for every programmer. This method...
4 min read
In JavaScript, adding elements to an array or list can be done in many ways. Every method has its unique application and easy accessibility. Every method is useful and choosing the best one makes the problem optimized. Let us see the methods that are used to...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India