JS中的“==”符號及布爾值轉換規則


what are the rules for how == converts types?

關於"=="的比較規則:

1. Comparing numbers and strings will always convert the strings to numbers. 

number類型與string類型比較,string會轉換為number類型。如:2=="2" true

2. null and undefined will always equal each other.

null類型與undefined類型比較始終相等。如:var a = null,b; 則 a==b 為true。

3. Comparing booleans to any other type will always cause the booleans to be converted to numbers.

布爾類型與其他任何類型進行比較,布爾類型將會轉換為number類型。如:var a = 0, b = false;則a==b為true

4. Comparing numbers or strings to objects will always cause the numbers or strings to be converted to objects.

number類型或者string類型與object類型進行比較,number或者string類型都會轉換為object類型。如:var a = 0, b = {};則a==b為false

 

The rules for converting other types to booleans are actually relatively straightforward:

關於其他類型轉換為布爾類型的規則:

1. undefined and null are always false.

2. Booleans are just treated as booleans (obviously).

3. Numbers are false if they equal 0 or NaN; otherwise, they’re true.

4. Strings are true, except for the empty string "", which is false.

5. Objects are always true.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM