JavaScript Object()
The Object() constructor creates objects.
Most JavaScript objects inherit properties and methods from Object.prototype.
The Object constructor is the root of the JavaScript object hierarchy.
Object is the archetype for all constructors.
It sits at the top of the JavaScript object hierarchy.
Unlike Array and RegExp, Object is both:
- The constructor for ordinary objects.
- The root prototype of almost all JavaScript objects.
Syntax
Object()
Object(value)
new Object()
new Object(value)
Parameters
| Parameter | Description |
|---|---|
| value | Optional. A value to wrap or convert to an object. |
Return Value
| Call | Returns |
|---|---|
Object() |
An object |
Object(value) |
An object representation of the value |
new Object() |
A new object |
Creating an Empty Object
Example
const person = new Object();
person.firstName = "John";
person.lastName = "Doe";
Object Literal (Recommended)
Object literals are the preferred way to create objects.
Example
const person = {
firstName: "John",
lastName: "Doe"
};
Object() as a Converter
This is where Object becomes more interesting than most constructors.
The Object() function can convert primitive values to wrapper objects.
Example
let s = Object("Hello");
let n = Object(123);
let b = Object(true);
Result
String object
Number object
Boolean object
Object Hierarchy
Most JavaScript objects inherit from Object.prototype.
Object ├─ Array ├─ String ├─ Number ├─ Boolean ├─ Date ├─ RegExp ├─ Error ├─ Map ├─ Set └─ Promise └─ Custom Objects
| Object Type | Inherits From |
|---|---|
| Array | Object |
| Error | Object |
| Boolean | Object |
| RegExp | Object |
| Error | Object |
| Map | Object |
| Set | Object |
| Promise | Object |
| Custom Objects | Object |
Common Object Methods
All ordinary objects inherit methods from Object.prototype.
toString()valueOf()hasOwnProperty()isPrototypeOf()
Use object literals ({}) instead of new Object() when possible.
Related Pages:
Browser Support
new Object() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |