[Web front-end basics | JS basics] Operators

1-Operator (operator)

1.1 Classification of operators

Operator (operator), also known as operator, is a symbol used to implement functions such as assignment, comparison, and execution of arithmetic operations.

Commonly used operators in JavaScript are:

  • Arithmetic Operator
  • Increment and decrement operators
  • Comparison operator
  • Logical Operators
  • Assignment operator

1.2 Arithmetic operators

  • Overview of arithmetic operators

    Concept: Symbols used in arithmetic operations to perform arithmetic operations on two variables or values.

symbol description
+ plus
- Less
* Multiply
/ except
% Take the remainder
  • The accuracy of floating point numbers

    The highest precision of floating-point values ​​is 17 decimal places, but its precision is far less than that of integers when performing arithmetic calculations.

  var result = 0.1 + 0.2;    // 结果不是 0.3,而是:0.30000000000000004
  console.log(0.07 * 100);   // 结果不是 7,  而是:7.000000000000001

So: do not directly judge whether two floating-point numbers are equal

  • Expression and return value

    Expression: is a combination of numbers, operators, variables, etc. in a meaningful arrangement method that can find values

    Simple understanding: is a formula composed of numbers, operators, variables, etc.

    The expression will eventually have a result, which is returned to the developer, called the return value

  • Operation rules
    First multiply and divide, then add and subtract, first parentheses, then calculate

1.3 Increment and decrement operators

  • Overview of increment and decrement operators

    If you need to repeatedly add or subtract 1 to a numeric variable, you can use the increment (++) and decrement (-) operators to complete. ++,-can be placed before or after the variable

    Note: The increment and decrement operators must be used with variables.

(●—●) increment operator

  • Pre-increment operator

    ++num is pre-incremented, which means adding 1 to itself, similar to num = num + 1, but ++num is easier to write.

    Use formula: add first, then return value

  var  num = 10;
  alert(++num + 10);   // 21
  • Post-increment operator

    num++ is post-incremented, which means it adds 1 to it, similar to num = num + 1, but num++ is easier to write.

    Use formula: return to the original value first, then add

var  num = 10;
alert(10 + num++);  // 20

1.4 Comparison operators

  • Overview of comparison operators

    Concept: A comparison operator (relational operator) is an operator used when comparing two data. After the comparison operation, a Boolean value (true/false) is returned as the result of the comparison operation.

symbol description
> Greater than (don’t compare strings, don’t compare pairwise)
< Less than (don’t compare strings, don’t compare pairwise)
>= greater or equal to
<= Less than or equal to
== Equal to (will convert type)18="18";//返回true,默认转换类型
!= not equal to
=== All equals (the same numeric types)
!== Not equal

1.5 Logical operators

  • Overview of logical operators

    Concept: Logical operators are operators used to perform Boolean operations, and their return values ​​are also Booleans. It is often used to judge multiple conditions in later development

symbol description
Logical and&& If both sides are true, return true, otherwise return false
Logical OR || If both sides are true, return true, otherwise return false
Logical negation Also known as the negation sign, it is used to take the opposite value of a Boolean value, for example, the opposite value of true is false

-Short circuit operation (logical interruption)

The principle of short-circuit operation: when there are multiple expressions (values), when the value of the expression on the left can determine the result, the value of the expression on the right is no longer calculated;

  • Logical and

    Syntax: expression 1 &&expression 2

    • If the value of the first expression is true, return expression 2

    • If the value of the first expression is false, return expression 1

    console.log( 123 && 456 );        // 456
    console.log( 0 && 456 );          // 0
    console.log( 123 && 456&& 789 );  // 789
    
  • Logical OR

    Syntax: expression 1 ||expression 2

    • If the value of the first expression is true, return expression 1

    • If the value of the first expression is false, return expression 2

    console.log( 123 || 456 );         //  123
    console.log( 0 ||  456 );          //  456
    console.log( 123 || 456 || 789 );  //  123
    

1.6 Assignment operator

Concept: Operators used to assign data to variables.

symbol description
= var num=1; Assign 1 to num
+= age += 5; // 相当于 age = age + 5;
-= Same as above
*= age *= 10; // 相当于 age = age * 10;
/= Same as above
%= Same as above

1.7 Operator precedence

  • The logical non-priority in unary operators is very high
  • Logic than logic or high priority
Parentheses ()
Unary operator ++;- -
Arithmetic Operator *; /; %; +; -
Relational operator >; >=; < ;<=
equal = ;!=; === ;!==
Logical Operators && ||
Assignment =
comma

【叽叽歪歪] It’s too messy, but I typed it all out, so I won’t delete it

1

Guess you like

Origin blog.csdn.net/qq_43490212/article/details/111240966