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.