logo

Converting any value to an object

June 6, 2021

Sometimes you might need to convert a value to an object. One of the ways to achieve this is by using the `Object()` function.

Here are some examples:

// A number num
let num = Object(7);
console.log(num); // Number {7}
typeof num; // "object"

// A string str
let str = Object("hi");
console.log(str); // String {"hi"}
typeof str; // "object"

// A boolean bool
let bool = Object(true);
console.log(bool); // Boolean {true}
typeof bool; // "object"

// An object obj
let obj = Object({ name: "Jo" });
console.log(obj); // {name: "Jo"}
typeof obj; // "object"

// null
let noValue = Object(null);
console.log(noValue); // {}
typeof noValue; // "object"

// undefined
let x = Object(undefined);
console.log(x); // {}
typeof x; // "object"