Home >Web Front-end >JS Tutorial >Take you step by step to understand the basics of JavaScript operators

Take you step by step to understand the basics of JavaScript operators

WBOY
WBOYforward
2021-10-12 10:27:321355browse

This article will take you through the basic knowledge of operators in JavaScript: arithmetic operators, increment/decrement operators, comparison operators, logical operators and ternary operators. I hope to be helpful.

Take you step by step to understand the basics of JavaScript operators

Operator

1 Arithmetic operator

  • Concept: It is to perform Operators for addition, subtraction, multiplication, and division, and remainder calculations

  • Operators: , -, *, /, % (remainder operations)

Note: When performing arithmetic operations, floating point (decimal) operations may cause precision problems

2 Increment and decrement operators

Operators: , -- (a, a--, a, --a)

//前置递增运算符
var num = 1;
++num //或者num++
console.log(num)//结果为2
 
//++num 相当于是 num = num + 1
//前置++ 是先做自增再做其他运算
 
 
 
 
//前置递减运算符
var num = 1;
--num //或者num--
console.log(num)//结果为0
 
//--num 相当于是 num = num - 1
//前置-- 是先做自减再做其他运算

3 Comparison operators

Operators: > , 28675c3d02b76e47536b84c673e04c48= , ebb451643671248cfef3bdb32bb50acb , 28675c3d02b76e47536b84c673e04c48= , <= The priority is 6


  • == , !=, ===, !== The priority is 7

  • ==: Determine whether the values ​​​​in the data on both sides are equal (different data types will be converted to the same Data type conversion 18=='18' // true)

  • ===: Whether the values ​​in the two passes of data are of the same type (18==='18' // false)

  • 4 Logical operator

    ##Logical AND (&&):

      If the && symbol is used, it means that only when both conditions are true (true), the result of the entire expression is true. As long as one condition is false, the expression The result of the formula is false
    • The interruption of logical AND:

      Because the AND operation requires all conditions to be established, the final AND operation result is true, if one of the conditions is not true, the result of the AND operation is false
    • var age = 18
      var num ;
      age>18 && (num = 998);
       
      //因为 age>18没有成立,逻辑与就已经得到结果为假
      //所以当逻辑与计算完毕之后,后面的num=998就不会再运行了
    • Logical OR (||):

      As long as there is one condition If it is true, the result of the expression is true. Only when all conditions are not true, the result of the expression is false.
    • Interruption of logical OR:
    var age = 18;
    var num;
    age == 18 || (num = 998);
     
     //因为 age==18成立,逻辑或就已经得到结果为真
    //所以当逻辑或计算完毕之后,后面的num=998就不会再运行了

    Logical NOT (!): Negate true to false, false to true

    var a = 5;
    !(a > 1)//a等于5,所以大于1为真(true),因为取反,所以这个表达式为假(false)

    5 Ternary operator: ?:

    can be understood as a simplified way of writing the double branch of if

    Grammar structure:

    表达式1 ? 表达式2 : 表达式3

    When expression 1 When it is established, expression 2

    will be executed. When expression 1 is not established, expression 3

    var a,b=2,c=3;
    a=b>2?b:c; //运行结果是a为3,b大于2为真就返回b给a,为假返回c给a,因为b不大于2,所以返回c给a

    will be executed. [Recommended learning:

    javascript advanced tutorial

    The above is the detailed content of Take you step by step to understand the basics of JavaScript operators. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete