In this tutorial, you will learn about the JavaScript constructor function and and the new Operator .how to use the new operator keyword objects. In OOPs programming , before objects are created a very limited way and we did was to use some simple object literal, and now that all changes with constructor functions. A difference between a regular function and a constructor function is that we call a constructor function with the new operator. OK, Let's go into details. In JavaScript, A constructor is a special function used to create an object instance of a class. 'use strict'; // constructor function const User = function (firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } //create an object const User = new User ('chandra' ,'kumar'); console.log(User); Output: chandra kumar In the above, the constructor is used to create a new object and set values of an existing object and the JavaScript calls the constructor ...
DailyAspirants: Your hub for free web development tutorials, SEO tips, and programming guides covering Python, SQL, C#, and more.