關於js中的‘==’ 與 ‘===’

LucineXL發表於2017-09-16

在js中,'==' 和 '==='運算子用來比較兩個值是否相等,但是他們對於相等的定義是不同的。兩個運算子都可以用來比較任意型別的運算元,如果兩個運算元相等,返回true,否則,返回false。'===' 嚴格相等運算子,用來比較兩個運算元是否嚴格相等。'==' 相等運算子,用來比較兩個運算元是否相等。
詳細資訊可參照ECMA標準([戳這裡][www.ecma-international.org/ecma-262/6.…

Abstract Equality Comparison ==

== 相等操作符,在比較前會把比較的兩個數轉換成相同的資料型別之後,然後對兩個數進行比較。轉換後,比較方式與 === 相同。

ECMA中比較規則如下:

The comparison x == y, where x and y are values, produces true or false.

1. ReturnIfAbrupt(x).
2. ReturnIfAbrupt(y).
3. If Type(x) is the same as Type(y), then
    Return the result of performing Strict Equality Comparison x === y.
4. If x is null and y is undefined, return true.
5. If x is undefined and y is null, return true.
6. If Type(x) is Number and Type(y) is String,
    return the result of the comparison x == ToNumber(y).
7. If Type(x) is String and Type(y) is Number,
    return the result of the comparison ToNumber(x) == y.
8. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
9. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then
    return the result of the comparison x == ToPrimitive(y).
11. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then
    return the result of the comparison ToPrimitive(x) == y.
12. Return false.複製程式碼

翻譯如下:

比較 x == y,當x 和 y是正常值時,返回 true 或者 false。

  1. 如果x不是正常值(比如丟擲一個錯誤),中斷執行。
  2. 如果y不是正常值,中斷執行。
  3. 如果Type(x)與Type(y)相同,執行嚴格相等運算x === y。
  4. 如果x為null且y為undefined,則返回true。
  5. 如果x為undefined,y為null,則返回true。
  6. 如果Type(x)是Number,Type(y)是String,返回比較結果 x == ToNumber(y)。
  7. 如果Type(x)是String,Type(y)是Number,返回比較結果ToNumber(x)== y。
  8. 如果Type(x)為Boolean,則返回比較結果ToNumber(x)== y。
  9. 如果Type(y)為Boolean,則返回比較結果x == ToNumber(y)。
  10. 如果Type(x)為String,Number或Symbol,Type(y)為Object,則返回比較的結果x == ToPrimitive(y)。
  11. 如果Type(x)是Object,Type(y)是String,Number或Symbol,那麼
    返回比較結果ToPrimitive(x)== y。
  12. 返回假。

簡化一下 ,可以理解為:

  1. 如果兩個運算元型別相同,則進行 x===y。
  2. 如果一個為null,另一個為undefined,則返回true。
  3. 如果兩個運算元均為基本資料型別,則把運算元轉換為Number型別進行比較。
  4. 如果其中有一個運算元為Object,則呼叫物件的 toString 或者 valueOf 方法,將物件轉化為原始值進行比較。
  5. 如果不滿足上述任何情況,則返回 false。

Strict Equality Comparison '==='

'===' 嚴格相等操作符,用來比較兩個運算元是否嚴格相等。

ECMA中比較規則如下:

1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is Number, then
    a. If x is NaN, return false.
    b. If y is NaN, return false.
    c. If x is the same Number value as y, return true.
    d. If x is +0 and y is −0, return true.
    e. If x is −0 and y is +0, return true.
    f. Return false.
5. If Type(x) is String, then
    a. If x and y are exactly the same sequence of code units (same length and same code           units at corresponding indices), return true.
    b. Else, return false.
6. If Type(x) is Boolean, then
    a. If x and y are both true or both false, return true.
    b. Else, return false.
7. If x and y are the same Symbol value, return true.
8. If x and y are the same Object value, return true.
9. Return false.複製程式碼

翻譯:

  1. 如果Type(x)與Type(y)不同,則返回false。
  2. 如果Type(x)為Undefined,則返回true。
  3. 如果Type(x)為Null,則返回true。
  4. 如果Type(x)是Number,那麼
    a. 如果x是NaN,則返回false。
    b. 如果y是NaN,則返回false。
    c. 如果x與y的Number值相同,則返回true。
    d. 如果x為+0且y為-0,則返回true。
    e. 如果x是-0而y是+0,則返回true。
    f. 返回假。
  5. 如果Type(x)是String,那麼
    a. 如果x和y是完全相同的程式碼單元序列(相同長度和相應索引處的相同程式碼單位),則返回true。
    b. 否則返回假。
  6. 如果Type(x)為Boolean,則
    a.如果x和y都為true或都為false,則返回true。
    b.否則返回假。
  7. 如果x和y是相同的符號值,則返回true。
  8. 如果x和y是相同的Object值,則返回true。
  9. 返回假。

簡化一下,可以理解為:

  1. 如果兩個運算元型別不相同,返回false。
  2. undefined === undefined => true
  3. null === null => true
  4. 如果運算元的資料型別都為Number,當兩個數的值相同時,返回true, 否則返回 false。
    注: -0 === +0 => true +0 === -0 => true
     NaN 與任何值都不相等,包括他自己。 所以要判斷一個數值是否為NaN, 可採用 x !== x ,只有NaN 返回true複製程式碼
  5. 如果運算元的資料型別都為String或Boolean時,只有x和y完全相同,返回ture。
  6. 如果運算元的資料型別都為Object,只有兩個運算元指向的地址完全相同時,返回true,否則返回false。

相關文章