Use Number.isFinite() to check for a number in JavaScript

Need the best way to check for a number in JavaScript while excluding NaN or Infinity? Try Number.isFinite() instead of typeof.

Dr. Derek Austin 🥳
1 min readDec 5, 2019

--

Hi , thanks for reminding me to take another look at ’s response — number.isFinite() is great!

I really appreciate you reading my article and for your response to it!

Indeed, Number.isFinite() is the most convenient way to check for a number in JavaScript, as it excludes NaN, Infinity, and -Infinity:

I updated my article on checking for a number in JavaScript to recommend Number.isFinite() and to note that there is also the global isFinite() function, which will coerce values to numbers before the check.

Photo by Karina Carvalho 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.

No responses yet