[javascript] Check if a value is an object in JavaScript

Ready to use functions for checking

function isObject(o) {
  return null != o && 
    typeof o === 'object' && 
    Object.prototype.toString.call(o) === '[object Object]';
}

function isDerivedObject(o) {
  return !isObject(o) && 
    null != o && 
    (typeof o === 'object' || typeof o === 'function') &&
    /^\[object /.test(Object.prototype.toString.call(o));
}

// Loose equality operator (==) is intentionally used to check
// for undefined too

// Also note that, even null is an object, within isDerivedObject
// function we skip that and always return false for null

Explanation

  • In Javascript, null, Object, Array, Date and functions are all objects. Although, null is bit contrived. So, it's better to check for the null first, to detect it's not null.

  • Checking for typeof o === 'object' guarantees that o is an object. Without this check, Object.prototype.toString would be meaningless, since it would return object for everthing, even for undefined and null! For example: toString(undefined) returns [object Undefined]!

    After typeof o === 'object' check, toString.call(o) is a great method to check whether o is an object, a derived object like Array, Date or a function.

  • In isDerivedObject function, it checks for the o is a function. Because, function also an object, that's why it's there. If it didn't do that, function will return as false. Example: isDerivedObject(function() {}) would return false, however now it returns true.

  • One can always change the definition of what is an object. So, one can change these functions accordingly.


Tests

_x000D_
_x000D_
function isObject(o) {_x000D_
  return null != o && _x000D_
    typeof o === 'object' && _x000D_
    Object.prototype.toString.call(o) === '[object Object]';_x000D_
}_x000D_
_x000D_
function isDerivedObject(o) {_x000D_
  return !isObject(o) && _x000D_
    null != o && _x000D_
    (typeof o === 'object' || typeof o === 'function') &&_x000D_
    /^\[object /.test(Object.prototype.toString.call(o));_x000D_
}_x000D_
_x000D_
// TESTS_x000D_
_x000D_
// is null an object?_x000D_
_x000D_
console.log(_x000D_
  'is null an object?', isObject(null)_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is null a derived object?', isDerivedObject(null)_x000D_
);_x000D_
_x000D_
// is 1234 an object?_x000D_
_x000D_
console.log(_x000D_
  'is 1234 an object?', isObject(1234)_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is 1234 a derived object?', isDerivedObject(1234)_x000D_
);_x000D_
_x000D_
// is new Number(1234) an object?_x000D_
_x000D_
console.log(_x000D_
  'is new Number(1234) an object?', isObject(new Number(1234))_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is new Number(1234) a derived object?', isDerivedObject(1234)_x000D_
);_x000D_
_x000D_
// is function object an object?_x000D_
_x000D_
console.log(_x000D_
  'is (new (function (){})) an object?', _x000D_
  isObject((new (function (){})))_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is (new (function (){})) a derived object?', _x000D_
  isObject((new (function (){})))_x000D_
);_x000D_
_x000D_
// is {} an object?_x000D_
_x000D_
console.log(_x000D_
  'is {} an object?', isObject({})_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is {} a derived object?', isDerivedObject({})_x000D_
);_x000D_
_x000D_
// is Array an object?_x000D_
_x000D_
console.log(_x000D_
  'is Array an object?',_x000D_
  isObject([])_x000D_
)_x000D_
_x000D_
console.log(_x000D_
  'is Array a derived object?',_x000D_
  isDerivedObject([])_x000D_
)_x000D_
_x000D_
// is Date an object?_x000D_
_x000D_
console.log(_x000D_
  'is Date an object?', isObject(new Date())_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is Date a derived object?', isDerivedObject(new Date())_x000D_
);_x000D_
_x000D_
// is function an object?_x000D_
_x000D_
console.log(_x000D_
  'is function an object?', isObject(function(){})_x000D_
);_x000D_
_x000D_
console.log(_x000D_
  'is function a derived object?', isDerivedObject(function(){})_x000D_
);
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to types

Cannot invoke an expression whose type lacks a call signature How to declare a Fixed length Array in TypeScript Typescript input onchange event.target.value Error: Cannot invoke an expression whose type lacks a call signature Class constructor type in typescript? What is dtype('O'), in pandas? YAML equivalent of array of objects in JSON Converting std::__cxx11::string to std::string Append a tuple to a list - what's the difference between two ways? How to check if type is Boolean

Examples related to javascript-objects

Cannot read property 'style' of undefined -- Uncaught Type Error Is this a good way to clone an object in ES6? What’s the difference between “{}” and “[]” while declaring a JavaScript array? Comparing two arrays of objects, and exclude the elements who match values into new array in JS Converting JavaScript object with numeric keys into array From an array of objects, extract value of a property as array How to copy JavaScript object to new variable NOT by reference? Remove property for all objects in array Using curl POST with variables defined in bash script functions How to sum the values of a JavaScript object?