Object() constructor
The Object
constructor turns the input into an object. Its behavior depends on the input's type.
-
If the value is
null
orundefined
, it will create and return an empty object. - Otherwise, it will return an object of a Type that corresponds to the given value.
- If the value is an object already, it will return the value.
When called in a non-constructor context, Object
behaves identically to
new Object()
.
Syntax
new Object()
new Object(value)
Parameters
value
-
Any value.
Examples
Creating a new Object
const o = new Object();
o.foo = 42;
console.log(o);
// Object { foo: 42 }
Using Object given undefined and null types
The following examples store an empty Object
object in o
:
const o = new Object();
const o = new Object(undefined);
const o = new Object(null);
Specifications
Specification |
---|
ECMAScript Language Specification # sec-object-constructor |
Browser compatibility
BCD tables only load in the browser