New Keyword

The Object constructor creates an object wrapper for a given value. If the value is null or undefined , it will create and return an empty object. Otherwise, it will return an object of a type that corresponds to the given value.

Objects can also be created using the new keyword. With the built-in Object Constructor in Javascript, new creates an empty object; or, this keyword can be used with a user-defined constructor function: with builtin Object Constructor .

To get started, first take a look at this example:

new Object()

The next step is to add properties and methods to this empty object. This can be achieved with simple dot notation:

However, this practice is not recommended, as there is a scope resolution behind the scenes to check if the constructor function is built-in or user-defined.

User-defined constructor functions

Functions can also be used to create objects in JavaScript. If you really think about it, they’re already objects—so basically, objects are used to create more objects.

Generally, this method is preferred over the object constructor. Imagine you have to create hundreds of objects with the same properties; with the object constructor method you’ll have to manually add all the properties to all the objects but with function constructor, these properties can be predefined.

Using the same function constructor, any number of objects can be created.