關於 == 和 ===的區別

1、=== 的判斷規則

一、若是類型不一樣,則不相等javascript

二、若是兩個都是數值,而且是同一個值,則相等。但例外的是 NaN !== NaN(能夠經過 isNaN() 來判斷是否爲 NaN)java

三、若是兩個都是字符串,每一個位置的字符都同樣,那麼相等,不然不相等數組

四、若是兩個值都爲true,或者都爲false,那麼相等函數

五、若是兩個值都引用同一個對象或函數,那麼相等ui

六、若是兩個值都是null,或者都是undefined,那麼相等spa

2、== 的判斷規則

一、同類型比較,進行 === 比較指針

二、若是兩個值類型不一樣,則根據以下規則進行類型轉換再進行比較:code

1)若是一個是字符換,一個是數字,則將字符串轉換成數字,再進行值比較

2)若是任一值是 true,把它轉換成 1 再比較;若是任一值是 false,把它轉換成 0 再比較。

3)若是一個是對象,另外一個是基礎類型(數值或字符串),則把對象轉換成基礎類型的值再比較。
   對象轉換成基礎類型,利用它的 toString 或者 valueOf 方法。

4)高級類型與高級類型,進行指針地址比較

5)若是一個是null,一個是undefined,那麼相等
複製代碼

3、關於類型轉換

一、any -> boolean
   number -> boolean   除了 0, -0, NaN 都爲 true
   string -> boolean   除了空串,都爲true
   undefined、null -> boolean  爲 false
   引用類型 -> boolean 都爲 true, [] -> true, {} -> true
   
二、any -> string
   number  -> string    5 -> '5'
   boolean -> string    true -> 'true'
   數組    ->  string   [1,2] -> '1,2'
   對象    ->  string   {...} -> '[object,Object]'

二、any -> number
   string -> number   '' -> 0, '1' -> 1, 'a' -> NaN
   boolean -> number    true -> 1, false -> 0
   數組 -> number   [] -> 0, [1] -> 1, [1,2] -> NaN (空數組轉爲0,存在一個元素且元素爲數字轉爲數字,其餘狀況轉爲NaN)
   null -> number  0
   undefined -> number NaN
   除了數組的引用類型 -> number 都爲NaN, {} -> NaN
複製代碼
  • 一、any -> boolean
// number -> boolean
    console.log(Boolean(0)); // false
    console.log(Boolean(-0)); // false
    console.log(Boolean(NaN)); // false
    console.log(Boolean(1111)); // true
    // string -> boolean
    console.log(Boolean('')); // false
    console.log(Boolean('11')); // true
    // undefined、null -> boolean
    console.log(Boolean(undefined)); // false
    console.log(Boolean(null)); // false
    // 引用類型 -> boolean
    console.log(Boolean([])); // true
    console.log(Boolean({})); // true
    console.log(Boolean([111])); // true
複製代碼
  • 二、any -> string
// number -> string
    console.log(String(0)); // '0'
    console.log(String(111)); // '111'
    // boolean -> string
    console.log(String(true)); // 'true'
    console.log(String(false)); // 'false'
    // 數組 -> string
    console.log(String([])); // ''
    console.log(String([1, 2])); // '1,2'
    console.log(String(['1', '2', '3'])); // '1,2,3'
    // 對象 -> string
    console.log(String({})); // '[object Object]'
    console.log(String({ a: 'a' })); // '[object Object]'
複製代碼
  • 三、any -> number
// string -> number
    console.log(Number('')); // 0
    console.log(Number('11')); // 11
    console.log(Number('a')); // NaN
    // boolean -> number
    console.log(Number(true)); // 1
    console.log(Number(false)); // 0
    // 數組 -> number
    console.log(Number([])); // 0
    console.log(Number([111])); // 111
    console.log(Number([111, 222])); // NaN
    console.log(Number(['a'])); // NaN
    // undefined、null -> number
    console.log(Number(undefined)); // NaN
    console.log(Number(null)); // 0
    // 除了數組的引用類型 -> number
    console.log(Number({})); // NaN
    console.log(Number(function () { })); // NaN
複製代碼

4、== 舉例

  • 一、若是一個是字符串,一個是數值,把字符串轉換成數值再進行比較。
console.log('111' == 111); // true
    console.log('a' == 1); // false
複製代碼
  • 二、若是一個是 null、一個是 undefined,那麼[相等]。
console.log(null == undefined); // true
複製代碼
  • 三、若是任一值是 true,把它轉換成 1 再比較;若是任一值是 false,把它轉換成 0 再比較。
console.log(true == 1); // true
    console.log(false == 0); // true
複製代碼
  • 四、高級類型與高級類型,進行指針地址比較
console.log({} == {}); // false
    const obj1 = {};
    const obj2 = obj1;
    console.log(obj2 == obj1); // true
    
    console.log([] == []); // false
    const arr1 = [];
    const arr2 = arr1;
    console.log(arr1 == arr1); // true
複製代碼
  • 五、若是一個是對象,另外一個是基礎類型(數值或字符串),則把對象轉換成基礎類型的值再比較。對象轉換成基礎類型,利用它的 toString 或者 valueOf 方法。
// 等號右邊:
    // ![] -> false -> 0 (說明: 全部引用類型轉化爲 boolean 都爲 true, 所以 [] -> true, ![] -> false, 
    // 若是任一值是 true, 把它轉換成 1 再比較。所以 false -> 0 )
    // 等號左邊:
    // [] -> 0 (說明:因爲等號右邊是number類型, 所以須要轉化爲number類型)
    console.log([] == ![]); // true
    
    
    // 等號右邊:
    // !{} -> false -> 0 (說明: 全部引用類型轉化爲 boolean 都爲 true, 所以 {} -> true, !{} -> false, 
    // 若是任一值是 true, 把它轉換成 1 再比較。所以 false -> 0 )
    // 等號左邊:
    // {} -> NaN (說明:因爲等號右邊是number類型, 所以須要轉化爲number類型)
    console.log({} == !{}); // false
    
    
    // 等號右邊: 字符串: '111,222' 
    // 等號左邊: [111, 222] -> '111,222' (說明:因爲等號右邊是 string 類型, 所以須要轉化爲 string 類型)
    console.log([111, 222] == '111,222') // true
    
    
    // 等號右邊: 數字: 111 
    // 等號左邊: [111, 222] -> NaN (說明:因爲等號右邊是 number 類型, 所以須要轉化爲 number 類型)
    console.log([111, 222] == 111) // false
    
    // 等號右邊: 字符串: '111' 
    // 等號左邊: [111, 222] -> '[object Object]' (說明:因爲等號右邊是 string 類型, 所以須要轉化爲 string 類型)
    console.log({} == '111') // false
    
    console.log({} == '[object Object]') // true

複製代碼

我的博客: www.cxdsimple.com/對象

相關文章
相關標籤/搜索