Using Object.prototype.toString.call() to check for a number in JavaScript

For numbers, the Object.prototype.toString.call() function is basically equivalent to using the typeof keyword.

Dr. Derek Austin 🥳
1 min readDec 5, 2019

--

Hi Stas Kovalenko, thanks for stopping by with your very useful response to my article on how to check for a number in JavaScript!

The JavaScript statement Object.prototype.toString.call() can indeed differentiate between numbers, objects, and even arrays, because it returns a string that specifies the object type in more detail than typeof.

Author Moon describes it well in Better Programming:

Indeed, your code example should always work the same as typeof:

Thanks for sharing! I think Object.prototype.toString.call() is the most generic way of checking data types in JavaScript, because it will differentiate between types of objects (such as arrays) and that can be useful at times.

Photo by dylan nolte on Unsplash

--

--

Dr. Derek Austin 🥳
Dr. Derek Austin 🥳

Written by Dr. Derek Austin 🥳

Hi, I'm Doctor Derek! I've been a professional web developer since 2005, and I love writing about programming with JavaScript, TypeScript, React, Next.js & Git.