JS: What's Object?

By Xah Lee. Date: . Last updated: .

A object is a collection of key/value pairs. A key/value pair is called property of the object.

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

Best way to undertand JavaScript object is to think of it as { • Python's DictionaryPerl's Hash TableRuby's Hash Table}, but with automatic inheritance feature.

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

〔►see JS: Data Types

For example, the following are all objects.

〔►see JavaScript Objects Reference Panel Index

Find Value's Data Type

How to check if something is a object?

Use typeof value.

function isObject (x) {
    if ( (typeof x === "object" || typeof x === "function") && x !== null) {
        return true;
    } else {
        return false;
    }}

console.log ( isObject( 3 ) ); // false
console.log ( isObject( null ) ); // false
console.log ( isObject( [] ) ); // true
console.log ( isObject( {} ) ); // true
console.log ( isObject( function () { } ) ); // true

〔►see JS: Data Types

// following are not objects

typeof undefined === "undefined"; // true

typeof "abc" === "string"; // true

typeof 3 === "number"; // true
typeof NaN === "number"; // true
typeof Infinity === "number"; // true

typeof true === "boolean"; // true
typeof false === "boolean"; // true

// --------------------------------

typeof null === "object"; // true
// Historical bug. 「null」 is not a object. Should return "null"

〔►see JS: null

Following are example of objects.

// 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 Objects Reference Panel Index

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, because they are JavaScript objects.

// array is a object, you can add properties to it
var aa = [3,4];
aa["p1"] = 7;
console.log(aa);                // [ 3, 4, p1: 7 ]
// function is object, you can add properties to it
var 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
var 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
var 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

Note: You should avoid adding properties to function, array, date, regex, etc. It is bad programing practice.

Object and Inheritance Topic

  1. JS: Object System Overview
  2. JS: What's Object?
  3. JS: Prototype and Inheritance
  4. JS: Create Object
  5. JS: Object Literal Expression
  6. JS: Find Object's Prototype
  7. JS: Set Object's Prototype
  8. JS: How to Create Object with Parent X?
  9. JS: Prevent Adding Property
  10. JS: Determine Type of Object
  11. JS: Primitive Value Object Wrapper
  12. JS: Clone, Deep Copy Object/Array
  13. JS: Test Equality of Objects

  1. JS: Object Object
  2. JS: Object.prototype
Like what you read? Buy JavaScript in Depth