JavaScript: Object Type

By Xah Lee. Date: . Last updated: .

What is Object

JavaScript spec defines object as: “a collection of key/value pairs”. (each pair is called a 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: array, function, date, regex.

Test If a Value is Object Type

Data Object (aka Object Object)

The data object, example: {a:1, b: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.

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. For a list of all builtin objects, see JavaScript Object Reference

You Can Add Properties to Any Object

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

// example of adding properties to different objects

// array
const aa = [3, 4];
aa["kk"] = 2;
console.log(aa.hasOwnProperty("kk"));

// function

const f1 = function () {
  return 3;
};

f1["kk"] = 2;
console.log(f1.hasOwnProperty("kk"));

// arrow function
const f2 = (() => 3);
f2["kk"] = 2;
console.log(f2.hasOwnProperty("kk"));

// date
const dd = new Date();
dd["kk"] = 2;
console.log(dd.hasOwnProperty("kk"));

// RegExp
const rx = /\d+/;
rx["kk"] = 2;
console.log(rx.hasOwnProperty("kk"));

JavaScript Object and Inheritance

BUY ΣJS JavaScript in Depth