JavaScript: Object Type

By Xah Lee. Date: . Last updated: .

JavaScript spec defines object as: “a collection of key/value pairs”.

A key/value pair is called property of the object. [see Property Overview]

Object is any value that is NOT any of { • undefinednulltruefalse • string • number (including NaN, Infinity) • symbol }.

[see Value Types]

For example, the following are all objects.

[see JavaScript Object Reference]

Data Object

The data object, example: {key1:1, key2:2}, is the best example of a collection of key/value pairs. We often call this object “object object”, or just “object”.

[see Object Object]

Special Purpose Objects

All objects other than the “object object” have special purposes and or hold internal data for that purpose.

For example,

They are all objects, but they are usually called by other names, such as function, array, regex, etc.

// all true

console.log( typeof {} === "object" );

console.log( typeof [3,4] === "object" );

console.log( typeof /\d+/ === "object" );

console.log( typeof (new Date()) === "object" );

console.log( typeof Math === "object" );

console.log( typeof (function () {}) === "function" ); // function is a special object

[see JavaScript Object Reference]

How to Check If Something is a Object?

“typeof” Operator

Determine Type of Object

Null

null is technically not a object. But typeof null return "object", due to a historical bug.

[see null]

Object is Key/Value Pairs

You can add properties to function, date, regexp, etc., even though they are not usually used as key/value pairs.

// array is a object, you can add properties to it
const aa = [3,4];
aa["p1"] = 7;
console.log(aa);                // [ 3, 4, p1: 7 ]
// function is object, you can add properties to it
const ff = function () {return 4;};
ff["p1"] = 1;
console.log(ff);                // { [Function] p1: 1 }
console.log(ff["p1"]);          // 1
// date is a object, you can add properties to it
const dd = new Date();
dd["p1"] = 7;
console.log(dd); // { Mon, 03 Mar 2014 09:01:47 GMT p1: 7 }
console.log(dd["p1"]);          // 7
// RegExp is a object, you can add properties to it
const rx = /\d+/;
console.log(rx.test("123"));    // true
rx["p1"] = 7;
console.log(rx);                // { /\d+/ p1: 7 }
console.log(rx["p1"]);          // 7

Note: You cannot add property to object that are not extensible. [see Prevent Adding Property]

JavaScript Object and Inheritance

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc