Number.isFinite() is the best way to check for a number in JavaScript

Need a simple way to check for a number in JavaScript, but don’t want NaN or Infinity? Use Number.isFinite() instead of typeof.

Dr. Derek Austin 🥳
1 min readDec 5, 2019

--

Hi , thanks for taking the time to read my article and for your insightful response!

You are exactly right, Number.isFinite() is the easiest way to check for a finite number, as it excludes NaN, Infinity, and -Infinity:

I clarified my article on checking for a JavaScript number to recommend Number.isFinite() and to point out that there is the global isFinite() function, which does perform type coercion if that behavior is desired:

Thanks again for supporting my blog and sharing your insight!

It is much appreciated!!! 😁

Photo by Joel Filipe 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.

Responses (1)