这篇文章主要介绍了JavaScript中equality(==)怎么用,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
神奇之处在哪里
最近负责的项目有涉及到前端的,所以尝试性的写了写js。在处理一个字段非空值的时候,用了 tagert_value == ''来进行判断,然后发生了一件非常奇怪的事情,有用户反馈,自己的target_value = 0的时候,非空值校验不通过。在调试问题的时候,在console状态栏中做了如下尝试:
> 0 == '' < true
我似乎知道问题出在哪里了。。。没有了解清楚 == 的判断逻辑,所以我打算找来官方的文档瞅瞅。
官方解释Equality (==, !=)
1、If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.
2、NaN is not equal to anything including itself.
3、Negative zero equals positive zero.
4、null equals both null and undefined.
5、Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.
6、Every other comparison is considered unequal.
本文题目:JavaScript中equality(==)怎么用-创新互联
URL标题:http://tyjierui.cn/article/deesgi.html