Member-only story

Mastering JavaScript’s Number Methods: .isNaN() and .isFinite()

A Practical Guide to Handling Numeric Values with Confidence

Max N
4 min readMar 29, 2024
Photo by Mick Haupt on Unsplash

In the world of JavaScript, working with numbers is a fundamental aspect of programming. However, dealing with numeric values can sometimes be tricky, especially when it comes to handling non-numeric inputs or special numeric values like infinity.

Fortunately, JavaScript provides two handy methods, .isNaN() and .isFinite(), to help you navigate these situations with ease. In this article, we'll dive deep into these methods, exploring their functionality and providing practical examples to solidify your understanding.

Understanding .isNaN()

The .isNaN() method is used to determine whether a value is the reserved value NaN (Not a Number). This method is particularly useful when you need to handle non-numeric inputs or operations that result in NaN. Here's how it works:

console.log(isNaN(42)); // Output: false (42 is a number)
console.log(isNaN('hello')); // Output: true ('hello' is not a number)
console.log(isNaN(NaN)); // Output: true (NaN is not a number)
console.log(isNaN(undefined)); // Output: true (undefined is not a number)

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet