Unary operator

Operators that operate on only one value are called unary operators

  1. Increasing and decreasing. The incrementing and decrementing operators are borrowed from C and come in two versions: pre – and post-

    1. Front-type: The operator precedes the variable to be operated on. Both pre-increment and decrement operations change the value of a variable before the statement is evaluated

      var age = 29;
      var anotherAge = --age + 2
      console.log(age) / / 28
      console.log(anotherAge) / / 30
      Copy the code
    2. Postposition: The operator comes after the variable to be operated on. Post-incrementing and decrementing operations are performed after the statements containing them have been evaluated

      var num1 - 2;
      var num2 = 20
      var num3 = num1-- + num2; / / 22
      var num4 = num1 + num2; / / 21
      Copy the code

    The above four operators work for any value, that is, strings, Booleans, floating-point values, and objects. Follow these rules:

    • When applied to a string containing a valid numeric character, it is converted to a numeric value before the addition or subtraction of 1 is performed. String variables become numeric variables
    • When applied to a string that does not contain numeric characters, set the value of the variable to NaN. String variables become numeric variables
    • When applied to a Boolean value false, it is converted to 0 before the addition or subtraction of 1 is performed. Boolean variables become numeric variables
    • When applied to a Boolean value true, it is converted to 0 before the addition or subtraction of 1 is performed. Boolean variables become numeric variables
    • Performs addition and subtraction of 1 when applied to floating point values
    • When applied to an object, the valueOf() method of the object is called to get a value that can be manipulated on, and then the above rules are applied to that value. In the case of NaN, the above rules are applied after the toString() method is called. Object variables become numeric variables

    Verify the above rules:

    var s1 = "2";
    var s2 = "z";
    var b = false;
    var f = 1.1;
    var o = {
        valueOf: function () {
            return -1}}console.log(s1++); / / 2
    console.log(s1); / / 3
    console.log(s2++); // NaN
    console.log(s2); // NaN
    console.log(b++); / / 0
    console.log(b); / / 1
    console.log(f--); / / 1.1
    console.log(f); / / 0.1000000000000009
    console.log(o--); // -1
    console.log(o); / / - 2
    Copy the code
  2. Unary addition and subtraction operators

    The unary addition and subtraction operators are used primarily for basic arithmetic operations. They can also be used to convert data types, that is, when applied to non-numeric values, the unary operator performs a conversion on the value as the Number() transformation function does. Boolean values false and true are converted to 0 and 1, and string values are parsed according to a special set of rules. Objects are converted from their valueOf() or toString() methods first. Take the unary plus operator for example:

    var s1 = "01";
    var s2 = "1.1";
    var s3 = "z";
    var b = false;
    var f = 1.1;
    var o = {
        valueOf: function () {
            return -1
        }
    }
    s1 = +s1
    s2 = +s2
    s3 = +s3
    b = +b
    f = +f
    o = +o
    console.log(s1) / / 1
    console.log(s2) / / 1.1
    console.log(s3) // NaN
    console.log(b) / / 0
    console.log(f) / / 1.1
    console.log(o) // -1
    Copy the code

Boolean operator

There are three Boolean operators: NOT, AND, OR

  1. Logic. By an English exclamation mark (!) Can be used for any value in ECMAScript. This operator returns a Boolean value regardless of the data type of the operand. A logical nonoperator first converts its operand to a Boolean value and then inverts it. Follow the rules

    • Return false if the operand is an object
    • Returns true if the operand is an empty string
    • If the operand is a non-empty string, return false
    • Returns true if the operand is the value 0
    • If the operand is any non-zero value (including Infinity), return false
    • Returns true if the operand is null
    • Return true if the operand is NaN
    • Returns true if the operand is undefined
    console.log(!false); // true
    console.log(!"blue"); // false
    console.log(!""); // true
    console.log(!0); // true
    console.log(!undefined); // true
    console.log(!null); // true
    console.log(!NaN); // true
    console.log(!12345); false
    Copy the code
  2. Logic and. The operator is represented by two amps (&&) and has two operands. Logic and operations can be applied to any type of operand, not just Boolean values. If one of the operands is not a Boolean, the logic and operation do not necessarily return a Boolean; In this case, it follows the following rules:

    • If the first operand is an object, return the second operand
    • If the second operand is an object, the object is returned only if the first operand evaluates to true
    • If both operands are objects, return the second operand
    • If the first operand is null, null is returned
    • NaN is returned if the first operand is NaN
    • Returns undefined if the first operand is undefined

    Logic and operations are short-circuited operations, that is, if the first operand determines the result, the second operand is not evaluated

  3. The logical or. Operator is made up of two vertical lines symbols (| |), said there are two operands. Like logic and operations, logic does not necessarily return a Boolean if one of its operands is not a Boolean; In this case, it follows the following rules:

    • Returns the first operand if the first operand is an object
    • The second operand is returned if the first operand evaluates to false
    • If both operands are objects, return the first operand
    • If both operands are null, null is returned
    • If both operands are NaN, NaN is returned
    • If both operands are undefined, undefined is returned

    Like logic and operators, logic or operators are short-circuiting operations. That is, if the first operand evaluates to true, the second operand is not evaluated

Multiplicative operator

ECMAScript defines three multiplicative operators: multiplication, division, and modulus. If one of the multiplicative operands is not a Number, it is first converted to a Number using the Number() function. For example, the empty string is treated as 0, and the Boolean true is treated as 1

  1. Multiplication. The operator is represented by an asterisk (*) and is used to compute the product of two numeric values. In the case of special values, the multiplication operator follows the following special rules:
    • If the operands are all numeric, performing normal multiplication computations, that is, two positive or two negative numbers multiply together will still yield a positive number, while if only one of the operands is signed the result will be negative. If the product exceeds the representation range of ECMAScript values, Infinity or -infinity is returned
    • If one of the operands is NaN, the result is NaN
    • If you multiply Infinity times 0, you get NaN
    • If Infinity is multiplied by a non-zero value, the result is Infinity or -infinity, depending on the sign of the signed operand
    • If you multiply Infinity times Infinity, you get Infinity
    • If one of the operands is not a value, call Number() in the background to convert it to a value, and then apply the above rules
  2. Division. The operator, represented by a slash (/), performs the calculation of the second operand divided by the first. Follow these rules:
    • If the operands are all numeric, perform the normal division calculation, that is, two positive or two negative numbers divided by the result is still positive; If only one of the operands is signed, the result is negative. Infinity or -infinity is returned if the ECMAScript value is out of the representation range
    • If one of the operands is NaN, the result is NaN
    • If Infinity is divided by Infinity, the result is NaN
    • If 0 is divided by 0, the result is NaN
    • If a nonzero finite number is divided by zero, the result is Infinity or -infinity, depending on the sign of the signed operand
    • If Infinity is divided by any non-zero value, the result is Infinity or -infinity, depending on the sign of the signed operand
    • If one of the operands is not a value, call Number() in the background to convert it to a value, and then apply the above rules
  3. Find the modulus (remainder). The operator is represented by a percent sign (%). Follow these rules:
    • If the operands are all numeric, perform the usual division calculation and return the remainder of the division
    • If the dividend is infinite and the divisor is a finitely large number, the result is NaN
    • If the dividend is a finitely large number and the divisor is zero, the result is NaN
    • If Infinity is divided by Infinity, the result is NaN
    • If the dividend is a finitely large number and the divisor is an infinite number, the result is the dividend
    • If the dividend is zero, the result is zero
    • If one of the operands is not a value, call Number() in the background to convert it to a value, and then apply the above rule

The additive operator

Like the multiplicative operator, the additive operator converts different data types behind the scenes.

  1. Addition. The addition operator is indicated by the plus sign (+). If both operands are numeric, perform normal addition and return the result according to the following rules:

    • If one of the operands is NaN, the result is NaN
    • If it’s Infinity plus Infinity, it’s Infinity
    • If it’s -infinity plus -infinity, it’s -infinity
    • If it’s Infinity plus -infinity, it’s NaN
    • If it’s plus 0 plus 0, it’s plus 0
    • If it’s minus 0 plus minus 0, it’s minus 0
    • If it’s plus 0 plus minus 0, it’s going to be 0

    If one of the operands is a string, the following rule applies:

    • If both operands are strings, concatenate the second operand with the first
    • If only one operand is a string, the other operand is converted to a string, and then the two strings are concatenated
    • If one of the operands is an object, a value, or a Boolean, their toString() method is called to get the corresponding string value, and the previous string rules apply
    • If undefined and null, call String() and get the strings “undefined” and “null”, respectively.
  2. Subtraction. The subtraction operator is represented by a minus sign (-) and, like the addition operator, follows the following rules:

    • If both operators are numeric, the normal arithmetic subtraction operation is performed and the result is returned
    • If one of the operands is NaN, the result is NaN
    • If it’s Infinity minus Infinity, it’s NaN
    • If it’s -infinity minus -infinity, it’s NaN
    • If it’s Infinity minus -infinity, it’s Infinity
    • If it’s -infinity Infinity Infinity, then it’s -infinity
    • If it’s plus 0 minus plus 0, it’s plus 0
    • If it’s minus 0 minus plus 0, it’s minus 0
    • If it’s negative 0 minus negative 0, it’s plus 0
    • If one of the operands is a string, Boolean, null, or undefined, the Number() function is now called in the background to convert it to a value, and then subtraction is performed according to the previous rules. If the result of the transformation is NaN, then the result of the subtraction is NaN
    • If one of the operands is an object, the valueOf() method of the object is called to get the value representing the object. If the value is NaN, the subtraction result is NaN. If the object does not have a valueOf() method, its toString() method is called and the resulting string is converted to a numeric value
    var result1 = 5 - true
    var result2 = NaN - 1
    var result3 = 5 - 3
    var result4 = 5 - ""
    var result5 = 5 - "2"
    var result6 = 5 - null
    console.log(result1) // 4, true is converted to 1
    console.log(result2) // NaN
    console.log(result3) / / 2
    console.log(result4) // 5, the empty string is converted to 0
    console.log(result5) // 3, string 2 is converted to the number 2
    console.log(result6) // 5, because null is converted to 0
    Copy the code

Relational operator

The relational operators are less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=). When the operands of relational operators use non-numeric values, they also perform data conversions or perform strange operations. The corresponding rules are as follows:

  • If both operands are numeric, a numeric comparison is performed
  • If both operands are strings, the character encoding values of the two strings are compared
  • If one operand is a value, the other operand is converted to a value, and a numeric comparison is performed
  • If an operand is an object, the valueOf() method of that object is called and the results are compared according to the previous rules. If the object does not have a valueOf() method, the toString() method is called and the result is used to perform the comparison according to the previous rules
  • If an operand is a Boolean, it is converted to a numeric value before the comparison is performed

When you compare strings, you are actually comparing the character code values for each character at the corresponding position in the two strings. After this comparison, a Boolean value is returned. The character encoding of uppercase letters is smaller than that of lowercase letters.

Equality operator

Two sets of operators: equal and unequal — convert first and then compare; Congruence and incongruence — only comparison, not conversion

  1. Equal and unequal. The equality operator is represented by two equals signs (==), and returns true if the operands are equal; The unequal operator is preceded by an exclamation point followed by an equal sign (! =), returns true if the operands are not equal. Both operators first convert operands (often called forced conversions) and then compare their equality. When converting different data types, follow these basic rules:

    • If an operand is a Boolean, it is converted to a numeric value before comparing equality — false to 0 and true to 1
    • If one operand is a string and the other is a value, the string is converted to a value before comparing equality
    • If one operand is an object and the other is not, the valueOf() method of the object is called and the resulting primitive type values are compared according to the previous rules

    The following rules apply when comparing the two operators

    • Null and undefined are equal
    • Null and undefined cannot be converted to any other value until equality is compared
    • If one of the operands is NaN, the equality operator returns false and the unequal operator returns true. The equality operator returns false even if both operands are NaN; Because NaN is not NaN by rule
    • If both operands are objects, they are compared to see if they are the same object. The equality operator returns true if both operands refer to the same object; Otherwise, return false
  2. Congruence and incongruence. The congruence operator, represented by three equals signs (===), returns true only if the two operands are equal without being converted. The incongruent operator has an exclamation mark followed by two equals signs (! ==), which returns true if the two operands are not equal without being converted.

    var result1 = ("55"= =55)
    var result2 = ("55"= = =55)
    var result3 = ("55"! =55)
    var result4 = ("55"! = =55)
    console.log(result1) // true
    console.log(result2) // false
    console.log(result3) // false
    console.log(result4) // true
    Copy the code

Conditional operator

Var Max = (num1 > num2)? num1 : num2

If num1 is greater than num2 (the relational expression returns true), assign the value of num1 to Max. If num1 is less than or equal to num2 (the relational expression returns false), the value of num2 is assigned to Max

The assignment operator

The assignment operator, represented by the equals sign (=), assigns the value on the right to the variable on the left. Compound assignment can be done if the multiplicative, additive, or bitwise operator is added before the equal sign. Each major arithmetic operator (and a few others) has a corresponding compound assignment operator. These operators are as follows:

  • Multiply/assign — *=
  • Assign by / — /=
  • Module/assignment — %=
  • Add/assign — +=
  • Subtract/assign — -=
  • Shift left/assign — <<=
  • Signed right shift/assignment — >>=
  • Unsigned right shift/assignment — >>>=

Comma operator

The comma operator can perform multiple operations in a single statement. For example, var num1=1, num2=2, num3=3; .

The comma operator can also be used for assignment. When used for assignment, the comma operator always returns the last item in an expression. Var num= (5, 6, 1, 4, 7, 0); // the num value is 0