When working with JavaScript, there are times when you need to verify if a variable contains a numeric value. While JavaScript is dynamically typed, which means you don't need to declare the data type of a variable, it's still essential to confirm the data type before performing operations to avoid errors. Here, we delve into the isNaN function provided by W3Schools, used to check if a value is Not-A-Number. Let's explore how to use it effectively in various scenarios.

isNaN is a global function in JavaScript that accepts a value as an argument and returns true if the value is not a number or is a non-finite number (e.g., Infinity, NaN, or a value obtained by dividing 0 by 0), and false otherwise.

Understanding isNaN
The isNaN function helps to validate user input, ensuring that the input can be used with numerical operations. It checks if the input is a number and whether it's a valid number or not.

Before moving forward, let's note that while the name implies checking if a value is 'Not a Number', it actually checks for both valid and invalid number inputs. If you want to check if a variable is of the number type, use the operator instead.
Basic Syntax

The basic syntax of the isNaN function is quite simple:
let result = isNaN(value);
Here, value is the input value that you want to check.
Example Usage

Let's see some examples of using isNaN:
- Checking a Number:
isNaN(10)will returnfalsebecause 10 is a valid number. - Checking NaN:
isNaN(NaN)will returntruebecauseNaNis not a valid number. - Checking a String:
isNaN("string")will returntruebecause "string" cannot be converted to a number. - Checking a Non-numeric String:
isNaN("365")will returnfalsebecause "365" can be converted to a number.
When to Use isNaN vs.

Now that we've explored the isNaN function, let's clarify when to use it and when to use the operator. Here's a simple rule of thumb:
- Use
isNaNto check if the input can be used in numerical operations, i.e., if it's a valid number or not. - Use
to check if the input is of thenumbertype, i.e., if the declared type of the variable is a number.



















isNaN is a simple yet powerful function, understanding its use cases can prevent unexpected behavior in your JavaScript code. Mastering this function will significantly enhance your data validation skills in JavaScript, helping you write robust and efficient applications.