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.

Octavian Theodor Nita, thanks for reminding me to take another look at Sumer Singh’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.

Image for post
Image for post
Photo by Karina Carvalho on Unsplash

Written by

πŸ€“ The physical therapist who writes JavaScript πŸ’ͺ Web Developer 😎 Mentor 🧠 DPT πŸ˜„ SEO Expert 😁 React πŸ˜† Jamstack πŸ—―οΈ Ask me anything πŸ‘‰ DoctorDerek.com πŸ‘ˆ

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store