JavaScript: 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. Best way to undertand JavaScript object is to think of it as {Python's Dictionary, Perl's Hash Table, Ruby's Hash Table}, but with automatic inheritance feature. ECMAScript 2015 §Overview#sec-terms-and-definitions-object

As a datatype, object is any value that is NOT {string, number (including NaN, Infinity) , true, false, null, undefined}. 〔►see JavaScript: Data Types

For example, the following are all objects.

Find Value's Data Type

How to check if something is a object?

Use typeof value.

var x = "something";

if ( typeof x === "object" && x !== null) {
    console.log("is object");
} else {
    console.log("is not object");
};

〔►see JavaScript: Data Types

// following are not objects

typeof "abc";                   // string

typeof 3;                       // number
typeof 3.5;                     // number
typeof NaN;                     // number
typeof Infinity;                // number

typeof false;                   // boolean

typeof undefined;               // undefined

typeof null;                    // object
// Historical bug. 「null」 is really not a object. Should be “null”

〔►see JavaScript: What's “null” Used For?

Following are example of objects.

// normal object
typeof {};                      // object

// array is a special object
typeof [3,4];                   // object

// RegExp is a special object
typeof /\d+/;                   // object

// date is a special object
typeof (new Date());            // object

// Math is a special object, hosting properties such as Math.PI, Math.sin ….
typeof Math;                  // object

// function is a special object
typeof (function () {return 3;}); // function

Object is Key/Value Pairs

You can add properties to function, date, regex. 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 has attribute extensible set to false. 〔►see JavaScript: Prevent Adding Property

Note: You should not add properties to function, array, date, regex, etc. It is bad programing practice, in general.

Object and Inheritance Topic

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

  1. JavaScript: Object Object
  2. JavaScript: Object.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.