JavaScript-邏輯運算子

    Coding

    JavaScript的運算子:
    在JavaScript中透過運算符號來賦予指定變數對應的數值。

    +----------+----------+-------------+
    | Name     | Abridge  | Mean        |
    +----------+----------+-------------+
    | =        | x = y    | x = y       |
    | +        | x += y   | x = x + y   |
    | -        | x -= y   | x = x - y   |
    | *        | x *= y   | x = x * y   |
    | /        | x /= y   | x = x / y   |
    | %        | x %= y   | x = x % y   |
    | **       | x **= y  | x = x ** y  |
    | <<       | x <<= y  | x = x << y  |
    | >>       | x >>= y  | x = x >> y  |
    | >>>      | x >>>= y | x = x >>> y |
    | &        | x &= y   | x = x & y   |
    | ^        | x ^= y   | x = x ^ y   |
    | |        | x |= y   | x = x | y   |
    +----------+----------+-------------+
    

    JavaScript的比較運算子:
    在JavaScript中遇到需要對數值進行運算、字串進行判斷時,
    可以運用比較運算來執行,同時需注意嚴謹的做法。

    +----------+-----------+--------+
    | Operator | Usage     | Result |
    +----------+-----------+--------+
    | ==       | 0 == '0'  | True   |
    |          | 0 == ''   | True   |
    +----------+-----------+--------+
    | ===      | 0 === '0' | False  |*
    |          | 0 ===  '' | False  |*
    +----------+-----------+--------+
    | !=       | 0 != '0'  | False  |
    |          | 0 !=  ''  | False  |
    +----------+-----------+--------+
    | !==      | 0 !== '0' | True   |*
    |          | 0 !==  '' | True   |*
    +----------+-----------+--------+
    | >        | 1 > 0     | True   |
    |          | 1 > 1     | False  |
    |          | 0 > 1     | False  |
    +----------+-----------+--------+
    | >=       | 1 >= 0    | True   |
    |          | 1 >= 1    | True  |
    |          | 0 >= 1    | False  |
    +----------+-----------+--------+
    | <        | 1 < 0     | False  |
    |          | 1 < 1     | False  |
    |          | 0 > 1     | True   |
    +----------+-----------+--------+
    | <=       | 1 <= 0    | False  |
    |          | 1 <= 1    | True   |
    |          | 0 <= 1    | True   |
    +----------+-----------+--------+
    

    JavaScript的邏輯運算子:
    在JavaScript中碰到需要判斷對與錯時,可以使用布林運算子來進行判斷。

    +----------+----------------+--------+
    | Operator | Usage          | Result |
    +----------+----------------+--------+
    | &&       | True && True   | True   |
    |          | True && False  | False  |
    |          | False && False | True   |
    +----------+----------------+--------+
    | ||       | True || True   | True   |
    |          | True || False  | True   |
    |          | False || False | False  |
    +----------+----------------+--------+
    | !        | !True          | False  |*
    |          | !False         | True   |*
    +----------+----------------+--------+
    

    JavaScript的解構賦值:

    var num = ['I', 'II', 'III'];
    
    // 不使用解構
    var I   = num[0];
    var II  = num[1];
    var III = num[2];
    
    // 使用解構
    var [I, II, III] = num;
    

    JavaScript的三元運算子:

    function getFee(isMember) {
      return (isMember ? '$2.00' : '$10.00');
    }
    
    console.log(getFee(true));
    // expected output: "$2.00"
    
    console.log(getFee(false));
    // expected output: "$10.00"
    
    console.log(getFee(null));
    // expected output: "$10.00"
    

    參考資料來源:JavaScript mdn