javascript - How can you reliably test if a value is equal to NaN? -
i know use isnan test if value equal nan. reliable?
the nan property represents value “not number”. special value results operation not performed either because 1 of operands non-numeric (e.g., "abc" / 4), or because result of operation non-numeric(e.g., attempt divide zero).
while seems straightforward enough, there couple of surprising characteristics of nan can result in hair-pulling bugs if 1 not aware of them.
for 1 thing, although nan means “not number”, type is, believe or not, number:
console.log(typeof nan === "number"); // logs "true"
additionally, nan compared – itself! – false:
console.log(nan === nan); // logs "false"
a semi-reliable way test whether number equal nan built-in function isnan(), using isnan() imperfect solution.
a better solution either use value !== value, produce true if value equal nan. also, es6 offers new number.isnan() function, different , more reliable old global isnan() function.
Comments
Post a Comment