The most accurate way to check JS object's type?

javascript check if object
javascript typeof object
check valid object in javascript
javascript object type
is object in array javascript
js check if something is object
check if property is an object javascript
check if it's an object js

The typeof operator doesn't really help us to find the real type of an object.

I've already seen the following code :



Is it the most accurate way of checking the object's type?

The JavaScript specification gives exactly one proper way to determine the class of an object:;

javascript - The most accurate way to check JS object's type?, The typeof operator doesn't really help us to find the real type of an object. I've already seen the following code : Object.prototype.toString.apply(t). Question:. In the article below we're going to take a look at an easy, pure-JavaScript way to access a device's whereabouts without relying on any external dependencies or third-party services. Let's begin! Location sources. JavaScript offers a simple, yet powerful tool for locating devices in the form of the Geolocation API. It consists of a small set of

the Object.prototype.toString is a good way, but its performance is the worst.

Use typeof to solve some basic problem(String, Number, Boolean...) and use Object.prototype.toString to solve something complex(like Array, Date, RegExp).

and this is my solution:

var type = (function(global) {
    var cache = {};
    return function(obj) {
        var key;
        return obj === null ? 'null' // null
            : obj === global ? 'global' // window in browser or global in nodejs
            : (key = typeof obj) !== 'object' ? key // basic: string, boolean, number, undefined, function
            : obj.nodeType ? 'object' // DOM element
            : cache[key = ({})] // cached. date, regexp, error, object, array, math
            || (cache[key] = key.slice(8, -1).toLowerCase()); // get XXXX from [object XXXX], and cache it

use as:

type(function(){}); // -> "function"
type([1, 2, 3]); // -> "array"
type(new Date()); // -> "date"
type({}); // -> "object"

[JavaScript] Check if a variable is a type of an Object or Array?, So how can we check if a variable is of type array or object, Well that's the question we are here to Solve. I will show you not one but three different ways using  Till the date, there are 3 ways to check if an object has a property : Compare with typeof and undefined. Use hasOwnProperty method. Use in keyword.

Accepted answer is correct, but I like to define this little utility in most projects I build.

var types = {
   'get': function(prop) {
   'null': '[object Null]',
   'object': '[object Object]',
   'array': '[object Array]',
   'string': '[object String]',
   'boolean': '[object Boolean]',
   'number': '[object Number]',
   'date': '[object Date]',

Used like this:

if(types.get(prop) == types.number) {


If you're using angular you can even have it cleanly injected:

angular.constant('types', types);

Javascript Object methods every developer should know, Before we start, lets check out a typical object initialization in JS. We have some more ways to initialize a JS object - but this is the most common way developers​  The second most common way to create objects is with Object constructor. A constructor is a function used for initializing new objects, and you use the new keyword to call the constructor. var mango = new Object (); mango.color = "yellow"; mango.shape= "round"; mango.sweetness = 8; mango.howSweetAmI = function () { console.log("Hmm Hmm Good"); }

var o = ...
var proto =  Object.getPrototypeOf(o);
proto === SomeThing;

Keep a handle on the prototype you expect the object to have, then compare against it.

for example

var o = "someString";
var proto =  Object.getPrototypeOf(o);
proto === String.prototype; // true

Different Ways to Check if an Object is Empty in JavaScript, Object.keys will return an Array, which contains the property names of the object. If the length of the array is 0, then we know that the object is  isInstance requires two parameters: an object and a type. The real trick to how it works is that it checks if the object is from the same window and if not gets the object's window.

Determining with absolute accuracy whether or not a JavaScript , Perhaps more important, there's a failsafe workaround: simply compare These methods and objects are accessed through the global object, and it is Along the constructor -checking lines, you could check for other array methods like push​  In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

How to check if an object has a property properly in javascript, The execution of the previous snippets 1000000 (1M) times, shows that the in and hasOwnProperty methods take approximately twice than the  See: How to check for undefined in javascript?, and whether a variable is undefined and How to handle ‘undefined’ in javascript – Shog9 Aug 3 '10 at 8:47 3 That "duplicate" is about object properties, so some of the answers don't apply very well to this question, asking about variables.

Working with objects, The same way, JavaScript objects can have properties, which define their characteristics. list of zero or more pairs of property names and associated values of an object, You can define methods for an object type by including a method SyntaxError: test for equality (==) mistyped as assignment (=)? Simple objects. Obviously the simplest way to make an object in JavaScript is an object literal. var o = { x: 42, y: 3.14, f: function() {}, g: function() {} }; But there's a drawback. If you need to use the same type of object in other places, then you'll end up copy-pasting the object's structure and initialization. The fix 2) Factory functions.

Object.getOwnPropertyNames(), The Object.getOwnPropertyNames() method returns an array of all properties (​including non-enumerable properties except for those which use  The most accurate way to determine your true maximum heart rate and target heart rates is to participate in a graded exercise test performed by a doctor. It’s always best to talk with a doctor

  • Have a look at this article:…
  • Look at this post :…
  • Most accurate way is... not testing the type. Why do you need the types?
  • If you're looking for a specific type you would likely want to do something along the lines of: FormData()) === "[object FormData]" which would be true. You can also use slice(8, -1) to return FormData instead of [object FormData]
  • Is there any difference between using Object.prototype and {}?
  • maybe this has changed over the years, but MyCustomObject()) returns [object Object] whereas new MyCustomObject() instanceOf MyCustomObject returns true which is what I wanted (Chrome 54.0.2840.99 m)
  • @Maslow, I've ran into the same issue that you brought up. After looking through some documentation online, I ended up using new MyCustomObject().constructor === MyCustomObject.
  • It begs the question, why have they no wrapped this code in a more convenient method, or allowed an additional operator compile to this. I know you are only the messenger, but quite frankly that is awful.
  • That test on jsPerf isn't quite accurate. Those tests are not equal (testing for the same thing). E.g., typeof [] returns "object", typeof {} also returns "object", even though one is an object Array and the other is an object Object. There are many other problems with that test... Be careful when looking at jsPerf that the tests are comparing Apples to Apples.
  • Your type function is good, but look at how it does compared to some other type functions.