Contents
Nan in JavaScript is a value that represents Not-A-Number. It is used to indicate that a value is not a valid number, and is often returned when mathematical operations are performed on invalid data.
Checkout this video:
Introduction
NaN stands for Not-A-Number, and it represents a value that is not a number. In other programming languages, this value would be represented by null or None.
NaN is a unique value in JavaScript, and it is not equal to any other value, including itself. This can be confusing, because it means that you can’t test for NaN with the == or === operator. To test if a value is NaN, you can use the built-in isNaN() function.
One common way to get NaN is to try to convert a non-numeric value to a number, using the Number() function:
Number(“hello”) // NaN
Number(“”) // 0
What is Nan in JavaScript?
In JavaScript, Nan means Not a Number. It is a value that can be assigned to a variable. The typeof operator in JavaScript returns Nan when it is used on an undefined variable.
What is typeof Nan?
Most programming languages have a special value called Not-A-Number (NaN), which is used to indicate that a value is not a valid number. However, in JavaScript, the typeof operator does not recognize NaN as a number, and instead returns “number” for all values, including NaN.
What is the difference between null and Nan?
Nan is a JavaScript reserved global property that represents a value that is not a number. This can occur when trying to convert a non-numeric string to a number, perform arithmetic operations on two non-nums, or usebuilt-in methods that expect numeric input on values that are not numeric.
Null is also a reserved JavaScript global property that represents the intentional absence of any value. It is often used to represent an empty or default value. While null values are considered numbers, they are not equivalent to zero.
What is the difference between undefined and Nan?
Nan stands for “not a number”. After a variable has been declared and given the value of Nan, it will remain Nan until its value is changed. When evaluated, expressions that include Nan will always return false. For example, the following will all return false:
eval(“5 == 5”); // returns true
eval(“5 == ‘5’”); // returns true
eval(“5 === 5”); // returns true
eval(“NaN == NaN”); // returns false
eval(“NaN === NaN”); // returns false
Unlike undefined, which can be overridden by reassigning undefined to another value, trying to reassign Nan will not change the value of the variable. For example:
var foo = “some string”;
console.log(foo); // outputs “some string”
console.log(typeof foo); // outputs “string”
foo = undefined;
console.log(foo); // outputs “undefined”
console.log(typeof foo); // outputs “undefined”
var bar = 5;
console.log(bar); // outputs 5
console.log(typeof bar); // outputs “number”
bar = NaN;
console.log(bar); // outputs NaN
console.log(typeof bar); // outputs “number”
Conclusion
Nan in JavaScript is a value that represents “not a number”. It is used to indicate that a value is not defined or is otherwise not a valid number. In addition to being used as a return value for mathematical operations that produce an undefined result, it can also be passed as an argument to functions that expect a number but are unable to process it. When this happens, the function will usually return Nan as well.