Object Constructors

Overview.

The new Object() constructor creates an object wrapper for the given value. If the value is null or undefined, it will create an empty object and return it. This object contains a set of key-value pairs that can be used to store and retrieve data.

Syntax

The syntax for creating an object using the Object() constructor is as follows:

new Object(value);

Parameter: The value parameter is optional. If passed, it will be used as the initial value for the object. If not passed, an empty object will be created.

Examples:

// Creating an empty object
const obj1 = new Object();
console.log(obj1); // {}

// Creating an object with an initial value
const obj2 = new Object({ name: "John", age: 30 });
console.log(obj2); // { name: 'John', age: 30 }

Use cases with examples:

Creating an object with dynamic keys:

const key = "name";
const value = "John";

const obj = new Object();
obj[key] = value;

console.log(obj); // { name: 'John' }

Using new Object() constructor to create a custom object:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const john = new Person("John", 30);
console.log(john); // Person { name: 'John', age: 30 }

Notes

  • The new Object() constructor is not recommended for creating objects with predefined keys. Instead, you can create an Object literal directly, like this:
const obj = { name: "John", age: 30 };
  • In modern JavaScript, it is more common to use class syntax to define custom objects. However, the new Object() constructor can still be used for creating basic objects or for compatibility with older code.

Edit this page on Github

This page was updated on -