|
1 | 1 | // NaN (Not a Number)
|
2 | 2 |
|
3 | 3 | // What
|
4 |
| -// - `NaN` stands for "Not-a-Number." It's a special value in JavaScript that shows up when you do something that doesn't make sense with numbers. |
| 4 | +// - `NaN` stands for Not-a-Number. |
| 5 | +// - It's a standalone value in JavaScript that shows up when you do something that doesn't make sense with numbers. |
5 | 6 |
|
6 | 7 | // Why
|
7 |
| -// - JavaScript uses NaN to tell you when a math operation can't give a proper number answer, like dividing zero by zero. |
| 8 | +// - NaN is for when a math operation can't give a proper number answer, for example dividing zero by zero. At that time JavaScript will return NaN. |
8 | 9 |
|
9 | 10 | // How
|
10 | 11 | // - When you try to do math with things that aren't numbers, like dividing text by a number, you get NaN. It's like JavaScript's way of saying "I can't give you a real number because this doesn't work."
|
11 | 12 |
|
12 |
| -// Note |
13 |
| -// - Be careful with NaN. If you use it in a calculation, the final result will also be NaN. Also, comparing NaN with other NaN values using === won't work as expected. |
14 |
| - |
15 | 13 | // Example
|
16 | 14 | let division = 0 / 0; // This division results in NaN
|
17 | 15 | console.log(division); // Output: NaN
|
18 | 16 |
|
| 17 | +// Note |
| 18 | + |
| 19 | +// NaN is Number (But Short Of) |
| 20 | +// - In JavaScript, typeof NaN returns "number" even though it represents "Not a Number." |
| 21 | +// - This is because NaN is a special numeric value used to indicate an invalid result. |
| 22 | +// - It occupies a space within the number system but doesn't represent a valid mathematical value. |
| 23 | +// - Be careful with NaN. If you use it in a calculation, the final result will also be NaN. |
| 24 | + |
19 | 25 | // NaN in Comparisons
|
20 |
| -// - For example, NaN === NaN returns false, and NaN !== NaN returns true. To check if a value is NaN, use the isNaN() function or the Number.isNaN() static method. |
| 26 | +// - Interestingly, even NaN compared to itself using comparison operators like === or !== returns false. |
| 27 | +// - This is because NaN is not equal to anything, including itself. |
| 28 | +// - For example, NaN === NaN returns false, and NaN !== NaN returns true. |
| 29 | +// - To check if a value is NaN, use the isNaN() method or the Number.isNaN() static method. |
21 | 30 |
|
22 | 31 | let result = 0 / 0; // This division results in NaN
|
23 | 32 | console.log(result); // Output: NaN
|
24 | 33 | console.log(typeof result); // Output: "number"
|
25 | 34 |
|
26 |
| -console.log(isNaN(result)); // Output: true |
27 | 35 | console.log(result === NaN); // Output: false
|
28 | 36 | console.log(result !== NaN); // Output: true
|
29 | 37 |
|
| 38 | +// How to Handle NaN (Recommended) |
| 39 | +console.log(isNaN(result)); // Output: true |
30 | 40 | console.log(Number.isNaN(result)); // Output: true
|
31 | 41 |
|
32 | 42 | // - It's important to handle NaN in your code, especially when dealing with user input or calculations, to ensure that your program behaves as expected and doesn't produce unexpected results due to the presence of NaN.
|
0 commit comments