# JS 一般相等(==) ###### tags: `Javascript` `note` 在 Javascript 中我們作相等的判斷都是使用嚴格相等 `===` ,而一般相等與嚴格相等的差異是什麼? `==` 不能用嗎? 嚴格相等與一般相等的差別在於,嚴格相等不會幫你作**轉型**,所以 `==` 帶來的不確定性過高,你必須熟悉轉型規則才能掌握。而使用嚴格相等使你更容易追蹤錯誤,不必考量轉型而導致的非預期結果,減少大量的猜測。 我將參考 MDN 描述作每個規則的講解 [MDN Equality (==)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality#description) 1. If the operands are both objects, return `true` only if both operands reference the same object. 當比較的兩者皆為物件時,比較其參考,參考相同即為 `true`。 ```js const objA = { value: 20 }; const objB = { value: 20 }; console.log(objA == objB); // false console.log(objA == objA); // true ``` --- 2. If one operand is `null` and the other is `undefined`, return `true`. 不論順序,只要其中一個為 `null` 並且另一個為 `undefined`,即為 `true` ```js console.log(null == undefined) // true console.log(undefined == null) // true ``` --- 3. If the operands are of different types, try to convert them to the same type before comparing: 只要比較的兩者為不同型別,會嘗試轉成相同型別再比較,以下是轉型規則 1. When comparing a number to a string, try to convert the string to a numeric value. 比較兩者其一為 number,另一個為 string,會嘗試將 string 轉型成 number。 ```js console.log(0 == ""); // true new Number("") == 0 console.log(20 == "20"); // true console.log(0 == "a"); // false new Number("a") == NaN ``` 1. If one of the operands is a boolean, convert the boolean operand to 1 if it is `true` and +0 if it is `false`. 假設比較值其中之一為 boolean ,將其轉成數字 1 或 +0 ( `true` => 1, `false` => +0 )。 ```js console.log(0 == false); // true 0 == +0 console.log(1 == true); // true 1 == 1 console.log("" == false); // true 0 == +0 console.log("1" == true); // true 1 == 1 ``` 3. If one of the operands is an object and the other is a number or a string, try to convert the object to a primitive using the object's `valueOf()` and `toString()` methods. 假設一個比較對象物件,另一個對象為 number 或 string 時,會嘗試使用物件方法 `valueOf()` 跟 `toString()` 轉為原始型別。 ```js const string1 = "hello"; // type is string const string2 = String("hello"); // type is string const string3 = new String("hello"); // type is object const string4 = new String("hello"); // type is object console.log(string1 == string2); // true console.log(string1 == string3); // true console.log(string2 == string3); // true // 兩者皆為物件,套用兩者皆為物件的規則 console.log(string3 == string4); // false ``` --- 4. If the operands have the same type, they are compared as follows: 假設兩者皆為相同型別時,遵循以下幾種規則 1. String: return `true` only if both operands have the same characters in the same order. 兩者皆為 string 時,比較兩者是否為相同字串 ```js console.log("" == "") // true console.log("hello" == "hello") // true console.log("asd" == "321") // false ``` 2. Number: return `true` only if both operands have the same value. `+0` and `-0` are treated as the same value. If either operand is `NaN`, return `false`. +0 與 -0 在 JS 視為相等,並且當只要有任何一個為 **NaN** (Not a Number),都會是`false`。 ```js console.log(10 == 10) // true console.log(+0 == -0) // true console.log(NaN == 0) // false console.log(NaN == NaN) // false ``` 3. Boolean: return `true` only if operands are both `true` or both `false`.