JS: 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 JS: Property Overview]

ECMAScript® 2016 Language Specification#sec-terms-and-definitions

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

[see JS: Value Types]

For example, the following are all objects.

[see JavaScript Object Reference]

Data 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 object “object object”, or just “object”.

[see JS: 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,

Technically, 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?

JS: “typeof” Operator

JS: Determine Type of Object


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

[see JS: 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, but you can still do so.

// 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 JS: Prevent Adding Property]

Object and Inheritance Topic

  1. JS: Object Overview
  2. JS: Object Type
  3. JS: Determine Type of Object
  4. JS: Object Literal Expression
  5. JS: Prototype and Inheritance
  6. JS: Create Object
  7. JS: Create Object with Parent X
  8. JS: Get/Set Prototype
  9. JS: Prevent Adding Property
  10. JS: Clone, Deep Copy Object/Array
  11. JS: Test Equality of Objects
  12. JS: Object Object
  13. JS: Object.prototype

JS Types Topic

  1. JS: Value Types
  2. JS: Primitive Value
  3. JS: Object Type
  4. JS: “typeof” Operator
  5. JS: Determine Type of Object
  6. JS: Object.prototype.isPrototypeOf
  7. JS: Property Key “constructor”
  8. JS: “instanceof” Operator
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc