NaN เป็นค่าเดียวที่ไม่เท่ากับตัวเอง (NaN !== NaN) ดังนั้นไม่สามารถใช้ == หรือ === เปรียบเทียบได้
ตัวอย่างที่ 1: วิธีผิด (ใช้ ==)
<html> <body> <script>
var val = 54/"the";
document.write(val);
document.write("<br>");
if(val == NaN) {
document.write("NaN is equal to NaN");
} else {
document.write("check in another way");
}
</script> </body> </html>
ผลลัพธ์
NaN check in another way
ตัวอย่างที่ 2: วิธีถูกต้อง ใช้ Object.is() (ES6)
<html> <body> <script>
var val = 54/"the";
document.write(val);
document.write("<br>");
document.write(Object.is(val, NaN));
</script> </body> </html>
ผลลัพธ์
NaN true
Object.is() เปรียบเทียบค่าได้ถูกต้องแม้กับ NaN และ -0/+0