What is the use of isNaN function? with code example

 The isNaN() function in JavaScript is used to determine whether a value is "Not-A-Number" (NaN) or not. NaN is a special value in JavaScript that represents an undefined or unrepresentable value resulting from an arithmetic operation.

The isNaN() function takes one argument and returns a boolean value. If the argument is NaN or cannot be converted into a number, the function returns true. If the argument is a number or can be converted into a number, the function returns false.

Here is an example code snippet that demonstrates the use of isNaN() function:

javascript
let num1 = 10; let num2 = '20'; let num3 = 'abc'; console.log(isNaN(num1)); // false console.log(isNaN(num2)); // false (string '20' is converted to number 20) console.log(isNaN(num3)); // true (string 'abc' cannot be converted to a number)

In the above example, we have used the isNaN() function to check if the variables num1, num2, and num3 are NaN or not. As you can see, the first two variables are numbers or can be converted into numbers, so the function returns false. However, the third variable num3 is a string that cannot be converted into a number, so the function returns true.

Post a Comment (0)
Previous Post Next Post