Using JavaScript's typeof Operator with Objects

May 17, 2021

Objects can be a bit frustrating when using the typeof operator because typeof returns 'object' for both null and []. To handle these cases, you have a couple options. The first option is to hardcode an if statement as follows:

let test = { firstName: 'Masteringjs', lastName: 'io' };
if (typeof test === 'object' && !Array.isArray(test) && test != null) {
  // do some really cool stuff
}

Whether you use the Array.isArray() check depends on your use case. There are cases where you want to treat arrays as objects, and cases where you don't.

Another edge case is that typeof returns 'function' for functions, but functions are technically objects. There is no JavaScript primitive for functions, functions are objects that inherit from the global Function class. To check if a value is an object, including functions, you can use the instanceof operator as shown below.

const fn = () => {};

typeof fn; // 'function'
fn instanceof Object; // true

Checking for POJOs

This check will allow any object through, including objects that are instances of classes. If you need a more rigorous check as to check whether an object is a plain old JavaScript object (or POJO for short), you can use the below function.

function isPOJO(arg) {
  if (arg == null || typeof arg !== 'object') {
    return false;
  }
  const proto = Object.getPrototypeOf(arg);
  if (proto == null) {
    return true; // `Object.create(null)`
  }
  return proto === Object.prototype;
}

isPOJO({}); // true
isPOJO(Object.create(null)); // true
isPOJO(null); // false
isPOJO(new Number(42)); // false

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials