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 (aka Object Object)

The data object, e.g. {key1:1, key2:2}, is the best example of a collection of key/value pairs. We often call this data object or just object. JavaScript spec calls it object object.

[see Object Object]

Special Purpose Objects

All objects other than the “data 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.

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");

[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.hasOwnProperty("p1"));
// function is object, you can add properties to it

const ff = function () {
  return 4;
};

ff["p1"] = 2;
console.log(ff.hasOwnProperty("p1"));
// date is a object, you can add properties to it
const dd = new Date();
dd["p1"] = 7;
console.log(dd.hasOwnProperty("p1"));
// RegExp is a object, you can add properties to it
const rx = /\d+/;
rx["p1"] = 7;
console.log(rx.hasOwnProperty("p1"));

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

JavaScript Object and Inheritance

BUY
ΣJS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc