The front end small white study notes, if has the mistake please everybody big guy points out, extremely thanks!!

1. Mathematical operators

1.1 Additive Operators (including implicit conversions)

Add +

  • Either of the operands on either side of the plus sign belongs to a character type (String). The plus sign is used for String concatenation.

  • Count up

    • The Number type

      • The operands are numbers and are added;
      • If one of the operands is NaN, the result is NaN
    • Null/undefined/true/false/object/array => Perform implicit type conversion

      • Object implicit conversion rules:

        If the operand is an object, it is first converted to its original value (toPrimitive, the date object calls toString(), other objects call valueOf(),), and the resulting original value is no longer cast to a number or string. Under this constraint, the original value of an object is almost always a string (if you don’t override valuOf() or toString()) and then add.

      • Addition example:

      console.log('10' + 1);       / / '101'
      console.log(1 + 2 + '3');    / / '33'
      console.log(1 + NaN);        // NaN (Number)
      console.log(1 + true);       // 2 (Number)
      console.log(1 + false);      // 1 (Number)
      console.log(1 + null);       // 1 (Number)
      console.log(1 + undefined);  // NaN (Number)
      console.log(1 + []);       / / '1'
      console.log([] + 1);       / / '1'
      console.log([] + []);      / /"
      console.log(1 + [1.2.3]);  / / '11, 2, 3'
      console.log(1 + {});          // '1[object Object]'
      console.log({} + 1);          // '[object Object]1'
      console.log({} + {});         // '[object Object][object Object]'
      console.log(1 + {name:'li'}); // '1[object Object]'
      console.log([] + {});                // '[object Object]'
      console.log({} + []);                // '[object Object]'
      console.log([1.2.3] + {name:'li'});  / / '1, 2, 3 [object object]'
      console.log(new Date() + 1);         // 'Fri Jul 09 2021 10:36:29 GMT+0800 1'
      Copy the code

Subtraction –

  • The Number type

    • Operands are numbers and are subtracted;
    • If one of the operands is NaN, the result is NaN
  • String, null, undefined, true, false

    • If the operand type is not Number, the implicit type conversion is performed to Number.

    Backup: Addition and subtraction are additive operators. A -b can be thought of as a+(-b).

1.2 Multiplier operators

Multiplication *

  • The Number type

    • The operands are all numbers and are multiplied;
    • If one of the operands is NaN, the result is NaN
  • String, null, undefined, true, false

    • If the operand type is not Number, it is converted to Number according to the implicit type conversion before operation.

Division /

  • Same as above

  • The divisor cannot be 0, otherwise the result is Infinity or -infinity

  • The dividend and divisor are both 0, and the result is NaN

    console.log(1 / 0); // Infinity
    console.log(0 / 1); / / 0
    console.log(0 / 0); // NaN
    Copy the code

Modulus %

  • The Number type

    • If the left and right sides are numeric, the division calculation is performed and the remainder is returned
    • NaN on either side, the result returns NaN
    • As long as the divisor is 0, the result returns NaN
  • If one side is not of the Number type, it is converted to a Number type (according to the corresponding rules) before calculation.

    console.log(1 % 0) // NaN
    console.log(0 % 1) / / 1
    console.log(0 % 0) // NaN
    Copy the code
  • Case: Used to judge odd and even, table interlaced color

1.3 Unary operators

Plus plus, minus minus

  • Operands are of type Number and have no effect (both positive and negative signs have no effect on NaN)
  • Operands are other types that can be converted to Number and have the same effect as Number(…). The same.

    console.log(+[]);    / / 0
    console.log(+[1]);   / / 1
    console.log(+[1.2]); // NaN [1,2] the original value is '1,2', converted to Number type NaN
    console.log(+{});    // NaN
    console.log(-undefined)  // NaN
    Copy the code

Implicit type conversion

var str = '123';
console.log(str + 2);    / / 1232
console.log(+str + 2);   // 125 => '123' converts to the Number type

var num1 = 10;
var num2 = 20;
console.log(num1 + num2);      / / 30
console.log(num1 + ' ' + num2); // 1020 => Convert to string concatenation
Copy the code
1.4 Increment decrement operator
  • ++ & —

  • Prefix type: Based on num1, perform the +1 operation and add num2

  • After num1 is added to num2, num1 does not increase before the calculation, and num2 increases after the calculation.

  • If one side is not of the Number type, it is converted to a Number type (according to the corresponding rules) before calculation.

1.5 case
  • 0.1 + 0.2 => (0.1 * 10 + 0.2 * 10) /10

2. The assignment operator

  • =, +=, -=, *=, /=, %=

3. Compare operators

3.1 Relational operators
  • <, >, <=, >=
  • The relational operator (the comparison operator) returns a Boolean
  • Number type, direct comparison; If NaN is present in the comparison, the result is false
  • Type String
    • On one side is a string, which is converted according to the corresponding rules
    • Both sides are strings, which are judged according to UniCode encoding. The comparison of strings is bit-by-bit, regardless of string length
  • Null, undefined, true, and false are converted according to the corresponding rules.
console.log('3' < 17);      // true Number('3') => 3 => 3<17
console.log('abc' < 3);     // false Number('abc') => NaN => NaN<3
console.log('3' < '17');    / / false (' 3 '). CodePointAt () < (' 1 '). CodePointAt () bit by bit
console.log('abc' < '3');   / / false (' a '). CodePointAt () < (' 3 '). CodePointAt () bit by bit
console.log(3 > 2 > 1);     // false From left to right, 3>2 is true, true>1 is false
Copy the code
3.2 Equality operators
  • == ==! = unequal, === congruent,! = = not congruent

equal

  • NaN is not equal to any type of value, including itself, and returns false
  • Both sides are strings to compare UniCode character encoding values
  • The values of type “Number” are on both sides. Check whether the values are the same
  • If both sides are Object, the IP addresses are the same
  • Null == undefined returns true
  • One side is String, one side is Number, convert String to Number and compare
  • If Boolean is on one side, the Boolean value is converted to Number and then compared.
console.log('ABC'= ='abc');  // false
console.log(NaN= =NaN)       // false
console.log('123'= =123)     // true
console.log(null= =undefined)// true
console.log(false= =0)       // true

let obj1 = {x:10};       
let obj2 = {x:10};
let obj3 = obj1;
console.log(obj1 == obj2);    // false
console.log(obj1 == obj3);    // true
Copy the code

Are congruent

  • Are the values equal
  • Type equality

Inequality and incongruence are the opposite of equality and congruence

4. Logical operators

With &&

  • Look at the operand on the left: perform an implicit conversion to Boolean, true/false

    • False, return the left-hand operand

    • True returns the right-hand operand

Or | |

  • Look at the operand on the left: 0, convert it implicitly to Boolean, true/false

    • True returns the left-hand operand

    • False, returns the right-hand operand

Non!

  • If I take the inverse, and only apply to one operand, the result must be a Boolean
  • !!!!! Boolean()
  • Operates on a condition and is therefore a member of the unary operator
console.log(1 && 2);    / / 2
console.log(0 && 2);    / / 0
console.log(1 || 2);    / / 1
console.log(0 || 2);    / / 2
console.log(!true);     // false
console.log(!0);        // true
console.log(!!100);     // true
console.log(! {});// false => Object type, return false (all objects are true)
console.log(!' ');       //true => Empty string, return true
console.log(!'abc');    // false => non-empty string, the result returns false
console.log(!NaN);      // true => NaN, return true
console.log(!100);      // false => non-zero value, return false
console.log(!null);     // true => null or undefined, return true
console.log(!undefined);// true
Copy the code