Skip to content

Determining Whether Your Javascript Object is of Type X

Erica Pisani
Erica Pisani
3 min read

There are times when you’re interested in knowing additional details about the Javascript Object that you’re handling, such as whether you’re handling an Array, Date, or standard Object. However, the typeof operator, when used against these different objects, returns the following results:

console.log(typeof [1,2,3]) // returns 'object'
console.log(typeof {hello: 'world'}) // returns 'object'
console.log(typeof new Date()) // returns 'object'

Why?

A major pitfall of using the typeof operator in order to determine the type of objects such as Array and Date is that they return 'object’. For more details, you can view the spec here, but the tl;dr is that all objects except functions and “non-standard exotic”, non-callable objects will return object when passed to typeof.

So how do I know what object I’m dealing with?

Fortunately, there’s a way to get values such as 'array' , 'object' , 'date' , etc. from those objects, and that’s where Object.prototype.toString.call() comes in.

How does it work?

The toString method is available to all objects that are children of Object , which Array and Date are. The inherited behaviour of toString, where [object <type>] is returned, is usually overridden by the more specialized objects. For instance, when the toString method is called from a Date object, we would get “Tue Apr 10 2018 09:23:37 GMT-0400 (EDT)”.

In order to get [object Date] as opposed to the date as a string, you would need to use the call method in order to to make the Date object the this value for the Object.prototype.toString function. This is important given how Object.prototype.toString evaluates what to return when called:

Object.prototype.toString evaluation steps

In other words, going back to our example from earlier, by using Object.prototype.toString.call to determine the type of our objects, we would get the following results:

console.log(Object.prototype.toString.call([1,2,3])); // Returns "[object Array]"
console.log(Object.prototype.toString.call({hello: 'world'})); // Returns "[object Object]"
console.log(Object.prototype.toString.call(new Date())); // Returns "[object Date]"

How can I determine whether my object is a particular class type?

The instanceof operator can help you here. You can learn more on the MDN page for this operator.

Wrapping up

Object.prototype.toString.call can be used to create a small utility function in your project that serves as an alternative to typeof similar to what’s seen in this great blog post. This will remove the need to repeatedly write code such as Object.prototype.toString.call(anArrayObj) === “[object Array]”.

Happy coding everyone!


📫
Enjoy this post? Subscribe to be notified when I publish new content!

Resources used for this article

web developmenttips-and-tricks

Comments


Related Posts

Members Public

How to install nvim-ufo in LazyVim to enable foldable code blocks

A feature that I've found myself missing since making the switch to Vim as my editor of choice is the ability to easily fold and expand code blocks. I know this functionality is included in Neovim, but I couldn't understand the native implementation enough to use

How to install nvim-ufo in LazyVim to enable foldable code blocks
Members Public

Git Log's Hidden Gems: Using -S and -L for Powerful Code History Search

Ever needed to track down when a specific piece of code was first introduced in a project? As part of some refactoring I had to do recently, I needed to do just that for a variable on a Django model. I was already familiar with the basic git log command,

Git Log's Hidden Gems: Using -S and -L for Powerful Code History Search
Members Public

Using XOR to write concise conditionals

It's not uncommon that I sometimes write if statements where the overall conditional is true when both conditions are true or both are false. As an example, let's say I'm validating input from an API call where I'm updating information on a