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]

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

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

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.

// 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]

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Find Object's Type
  4. Prototype Chain
  5. Create Object
  6. Object Literal Expr
  7. Create Object with Parent X
  8. Get/Set Parent
  9. Show Prototype Chain
  10. Prevent Adding Property
  11. Clone Object
  12. Test Object Equality
  13. Add Method to Prototype

JS Types

  1. Value Types
  2. Primitive Value
  3. Object Type
  4. “typeof” Operator
  5. Find Object's Type
  6. Object.prototype.isPrototypeOf
  7. Property Key “constructor”
  8. “instanceof” Operator
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials