Is NaN equal to NaN?

前端 未结 8 2026
遇见更好的自我
遇见更好的自我 2020-11-27 05:22
parseFloat(\"NaN\")

returns \"NaN\", but

parseFloat(\"NaN\") == \"NaN\"

returns false. Now, that\'s probably a go

8条回答
  •  攒了一身酷
    2020-11-27 05:49

    isNaN works for all values that aren't numbers

    isNaN('foo') == true
    isNaN(NaN) == true
    isNaN(12) == false
    isNaN([1,2,3]) == true
    

    If, however you want to check for NaN specifically, or avoid type coercion;
    you can use Number.isNaN instead

    Number.isNaN('foo') == false
    Number.isNaN(NaN) == true
    Number.isNaN(12) == false
    Number.isNaN([1,2,3]) == false
    

提交回复
热议问题