The operator

Operators, also known as operators, are conjunctions that perform functions such as assignment, comparison, and arithmetic operations.

Arithmetic operator

An overview of the

  • The arithmetic operators are:+ - * / %

Floating point precision problem

Floating-point numbers have a maximum accuracy of 17 decimal places, but are far less accurate than integers in arithmetic calculations.

  • Floating-point arithmetic has problems

    console.log(0.1+0.2);/ / 0.30000000000000004
    console.log(0.007*100);/ / 7.000000000000001
    var num=0.1+0.2;
    console.log(num==0.3);//false
    Copy the code

Expressions and return values

Expression: an expression consisting of numbers, operators, variables, etc. The name of a function is also a variable. Function name variables are usually used to return the processing result data of a function to the calling function, that is, recursive call. Therefore, function name variables are generally called return values. The return value type of a function is specified when the function is defined.

Increment and decrement operators

Use the ++ — operator to repeatedly add or subtract 1 from a numeric variable. In JS, increment and decrement can be placed either before a variable (the pre-increment/subtraction operator) or after it (the post-> increment/subtraction operator). Incrementing and decrement operators must be used in conjunction with variables.

Increasing pre – Increasing the rear Front of diminishing The rear of diminishing
++num num++ –num num–
The value is returned after +1 Return first and then +1 Returns the value after -1 Return the original value and then -1
  • Pre and post, used alone, give the same result. If used in conjunction with other code, the result of execution will be different: after the original value calculation and then add, before the first add, after the operation.
  • Most development uses post-increment.

Comparison operators (relational operators)

A comparison operator (relational operator) is an operator used to compare two pieces of data, returning a Boolean value (true/false) as the result of the comparison.

== converts the data type by default, converting string data to alphanumeric data (implicit conversion). === requires that the values and data types on both sides be exactly the same.

console.log(18= ='18');//true
console.log(18= = ='18');//false
Copy the code

Logical (Boolean) operator

&& || !

Short circuit operation (logical interrupt)

Principle: When there are more than one expression (value), the expression value on the left can determine the result, do not continue to calculate the expression value on the right.

Expression 1 && expression 2

  • If the value of the first expression is true, expression 2 is returned
  • If the value of the first expression is false, expression 1 is returned

Expression expression 1 | | 2

  • If the value of the first expression is true, expression 1 is returned
  • If the value of the first expression is false, expression 2 is returned

Note: null or negative is false, the rest is true 0 null “” undefined NaN

The assignment operator

= += -= *= /= %=

Operator priority

priority The operator The order
1 parentheses (a)
2 Unary operator ++ -- !
3 Arithmetic operator First,* /+ -
4 Relational operator > > = < < =
5 Equality operator = = ! = = = = ! = =
6 Logical operator First,&&After `
7 The assignment operator =
8 Comma operator .