Member-only story
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)