Numbers are a primitive type in JavaScript, but just using typeof will not differentiate numbers from NaN or Infinity.

Dr. Derek Austin 🥳
Sep 23 · 5 min read
Photo by Alex Chambers on Unsplash

It is useful to know how to check if a value is of the primitive type number, since JavaScript is a dynamically typed language.

One example when a number might show up as another type, indicating a bug in my code, would be in parsing currency.

In this case, I would know that I forgot to parse the string into a number when I get the value of “string” back from typeof.

Photo by Samuel Zeller on Unsplash

Catching a failed parse

If I use parseFloat() to check the currency value, but I forgot to strip the currency symbol, I get a failed parse and an odd result:

Oh, no. In this case, it looks like typeof is not working how I expected.

Photo by Gemma Evans on Unsplash

When the typeof keyword returns “number”

An obvious solution to check for the number primitive type is to use the typeof keyword, as I showed above.

But typeof is an incomplete solution, because of two special values in JavaScript: NaN (“Not a Number”) and Infinity.

For my currency example, I know I have a bug if my monetary value is NaN or Infinity, but typeof returns “number” for both these values.

Perhaps my input was written using a currency symbol, and I simply tried to use parseFloat() to capture the value. That would result in NaN:

Photo by Timo Kaiser on Unsplash

A robust isNumber() function

A more robust isNumber() function would check for NaN values, check for Infinite values, and use typeof to check for the number type:

Or, in the case where one is confident that NaN and Infinity are not going to come up, just checking that typeof==="number" is enough.

Photo by 🇨🇭 Claudio Schwarz | @purzlbaum on Unsplash

The one liner

I love one-liners. I think they need to be documented with comments, but I think they are actually really useful, not just fancy.

Here is the one-liner for how to check for a number in JavaScript:

Photo by Bernard Hermant on Unsplash

Check if a variable is an integer

To check if variable A is an integer I could use the loose equality operator == to see if the parsed value equals itself.

That would look like the following code example:

Using typeof would differentiate a string that is being coerced into a number from an actual number, if that is important to you.

Photo by Markus Spiske on Unsplash

Strip out currency symbols

For a better, more robust function, I might want to strip out currency symbols (such as the dollar sign and any commas) first.

Thoroughly parsing money using regular expressions in JavaScript is beyond the scope of this article, but this code removes $ and , before parsing:

Photo by Toa Heftiba on Unsplash

Does JavaScript actually have separate integers?

No, JavaScript only has one type of number, which is represented internally as a 64-bit floating point representation.

That floating point is the primitive data type number, and there is also a type called BigInt that can be used for arbitrarily large numbers.

The functions String.prototype.parseInt() and String.prototype.parseFloat() differ in what they expect and will output, but not because there are actually separate integer and floating point types in JavaScript.

Photo by Volkan Olmez on Unsplash

Conclusion

Checking for a number in JavaScript is not particularly complicated — typeof works basically how it should, as long as one is aware that both NaN and Infinity have the typeof number.

Checking for an integer specifically involves the parseInt() function.

Under the hood, integers and floats are all just numbers in JavaScript.

Photo by K. Mitch Hodge on Unsplash

Further reading

  • The regular expression ^[0–9]+(\.[0–9]{1,2})?$ matches money:
Photo by Alejandro Ortiz on Unsplash

JavaScript in Plain English

Learn the web's most important programming language.

Dr. Derek Austin 🥳

Written by

😀 Full-Stack Web Developer 🤓 JavaScript 😄 React 😁 React Hooks 😆 Jest 🥰 CodeSandbox.io Contributor 🧠 Mobile App Developer 🤳 Doctor of Physical Therapy 🆒

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade