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》

相關文章
相關標籤/搜索