Skip to main content Accessibility Feedback


More accurately check the type of a JavaScript object.

Source Code


let arr = trueTypeOf([]); // array
let obj = trueTypeOf({}); // object
let str = trueTypeOf(''); // string
let date = trueTypeOf(new Date()); // date
let num = trueTypeOf(1); // number
let fn = trueTypeOf(function () {}); // function
let reg = trueTypeOf(/test/i); // regexp
let bool = trueTypeOf(true); // boolean
let nl = trueTypeOf(null); // null
let undef = trueTypeOf(); // undefined

The helper function

 * More accurately check the type of a JavaScript object
 * (c) Chris Ferdinandi, MIT License,
 * @param  {Object} obj The object
 * @return {String}     The object type
function getType (obj) {
	return, -1).toLowerCase();

How it works

You may already be familiar with the typeof operator, which returns a string letting you know the type of a JavaScript object.

// returns "number"
typeof 123;

But because most things are objects in JavaScript, it can return some pretty weird results.

typeof []; // object
typeof {}; // object
typeof ''; // string
typeof new Date() // object
typeof 1; // number
typeof function () {}; // function
typeof /test/i; // object
typeof true; // boolean
typeof null; // object
typeof undefined; // undefined

The array, plain object, date, regex, and null all return object. The only really accurate ones are the string, function, boolean, and undefined.

So… how do you accurately check the true type of an object with vanilla JS?

You pass the item that you want to check the type of into the method as an argument. This gets the item’s prototype and converts it to a string, which gives you back something like [object Array] or [object Boolean].[]); // [object Array]{}); // [object Object]''); // [object String] Date()); // [object Date]; // [object Number] () {}); // [object Function]; // [object RegExp]; // [object Boolean]; // [object Null]; // [object Undefined]

We can then use the String.prototype.slice() method to remove the square brackets ([]) and leading object string., -1).toLowerCase();