Chapter 3 Operators of Basic Concepts

Unary operator:

Is a symbol that can only operate on one value

  • ++a
  • –a
  • +a
  • -a

Increment and decrement operators:

++ and –:
increment and decrement operators follow the following rules:

  • For string:
    If the string has numbers, first convert the string to a numeric value, and then perform the operation of adding and subtracting 1, and then the string becomes a numeric variable.
    If there is no number, then the string variable becomes NaN. When the string becomes a numeric variable.
  • For Boolean values, first convert true/false to 1/0, and then perform the operation of adding and subtracting 1, at which time the Boolean value becomes a numeric variable.
  • For an object, first call the valueOf() method of the object, and then apply the above rules to it. If the result is NaN, then call the toString() method and then apply the above rules, and the object variable becomes a numeric variable.

E.g:

var o={
    
    
    valueof:function(){
    
    
      return -1;
    }
};
o--;   //值变为-2;

Unary addition and subtraction operators

When applying a unary operator to a non-numeric type, the operator will perform the conversion on the value like the Number() conversion function.
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

Boolean operator

There are three types: and, or, not

Logical negation:

  • If the operand is an object, return false;
  • If the operand is an empty string, return true;
  • If the operand is a non-empty string, return false;
  • If the operand is the value 0, return true;
  • If the operand is any non-zero value, return false;
  • If the operand is null, return true;
  • If the operand is NaN, return true;
  • If the operand is undefined, return true;
         alert(!false);//true  
         alert(!"blue");//false
         alert(!0);//true
         alert(!NaN);//true
         alert(!"");//true
         alert(!12345);//false

Logical AND (&&)

  • As long as there is a false, it must be false, if the first is false, the second number will not be judged
  • Cannot use undefined values ​​in logical AND
  • If the first operand is an object, return the second operand
  • If the second number is an object, only return the second object when the first number is true
  • If both operands are objects, return the second number
  • If the first number is null, NaN, undefined, then return null, NaN, undefined

Logical OR (||)

  • If the first operand is an object, return the first operand
  • If the first operand is false, return the second operand
  • If both operands are objects, return the first number
  • If both numbers are null, NaN, undefined, then return null, NaN, undefined

Guess you like

Origin blog.csdn.net/weixin_42898315/article/details/111058383