a = [1, 2, NaN];
for (const item of a) {
if (item === NaN) {
console.log(true);
}
}
I want to be able to print true
to the console.
Why can't item
match up to NaN?
a = [1, 2, NaN];
for (const item of a) {
if (item === NaN) {
console.log(true);
}
}
I want to be able to print true
to the console.
Why can't item
match up to NaN?
Hilariously, in javascript NaN does not equal NaN. That means that you can check for NaN by doing if(foo !== foo)
. Normally you should use the isNan() function instead of that because it's way more readable.
Because NaN is the only thing in Javascript not equal to itself. isNaN()
a = [1, 2, NaN];
for (const item of a) {
if (isNaN(item)) {
console.log(true);
}
}