JS数据值的严格比较与抽象比较

#等值比较操做


WHAT
javascript

「严格值比较」:===
java

「抽象值比较」或自动转换比较:==
ide

`比较操做的规则

==先转换类型再比较,===先判断类型,若是不是同一类型直接为false。
.net

「严比」是类型和值都进行比较,「抽象比较」则比较的是抽象语义,例如空串与0的语义类似,可当作相等
3d

`抽象值比较在比较操做前会自动转换类型,转换方式(何种类型被转换)取决某种规则,例如"10" + 10,字符转为数值

JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison.
cdn

#大小值比较,先转为基础类型值(primitives),再转像类型,再比较

For relational abstract comparisons (e.g., <=), the operands are first converted to primitives, then to the same type, before comparison.
htm

WHY HOW

个人建议是:若是你的的确确知道你在作什么(了解类型转换的结果),能够用==;不然仍是用===吧。
blog

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operatorsip

https://stackoverflow.com/questions/5447024/javascript-comparison-operators-identity-vs-equality
it

JavaScript中三个等号和两个等号你了解多少

https://www.jb51.net/article/117815.htm

ES6 Object.is()

大部分状况下,你仍是可继续三等号,除非很是特殊的须要,才考虑使用Object.is()

《U ES6》

相关文章
相关标签/搜索