Junior — Middle
Mida tähendab NaN väärtus JavaScripti kontekstis?
sobes.tech AI
Vastus AI-lt
In JavaScript, the value NaN means "Not-a-Number". It is a special numeric value indicating that the result of a calculation is not a valid number.
NaN occurs, for example, when attempting to perform arithmetic operations with non-numeric values that cannot be converted to a number:
console.log(0 / 0); // NaN
console.log(parseInt("abc")); // NaN
console.log(Math.sqrt(-1)); // NaN
Characteristics of NaN:
NaNis of typenumber.NaNis not equal to anything, including itself:NaN === NaNreturnsfalse.- To check if a value is
NaN, use theisNaN()function or, more reliably,Number.isNaN().
Example of checking:
let value = NaN;
console.log(value === NaN); // false
console.log(isNaN(value)); // true
console.log(Number.isNaN(value)); // true
Thus, NaN serves as an error indicator in numerical calculations and helps identify incorrect operations.