January 2020
Intermediate to advanced
470 pages
11h 13m
English
Now, for variety, a little quiz. Suppose you had an array of numbers and wanted to run a sanity check, studying whether any of them was NaN. How would you do this? A tip: don't try checking the types of the array elements—even though NaN stands for not a number, typeof NaN === "number". You'll get a surprising result if you try to do the search in an obvious way:
[1, 2, NaN, 4].findIndex(x => x === NaN); // -1
What's going on here? It's a bit of interesting JavaScript trivia: NaN is the only value that isn't equal to itself. Should you need to look for NaN, you'll have to use the new isNaN() function, as follows:
[1, 2, NaN, 4].findIndex(x => isNaN(x)); // 2
This was a particular case, but worth knowing about; I actually ...
Read now
Unlock full access