Comparing NaN values for equality in Javascript

前端 未结 12 1775
猫巷女王i
猫巷女王i 2020-12-13 08:19

I need to compare two numeric values for equality in Javascript. The values may be NaN as well. I\'ve come up with this code:

if (val1 == val2 |         


        
12条回答
  •  渐次进展
    2020-12-13 09:01

    As long as you know these two variables are numeric, you can try:

    if (val1 + '' == val2 + '')
    

    It turns the two values into strings. A funny answer, but it should work. :)

提交回复
热议问题