Web front-end, JS basic arithmetic, assignment, unary, comparison, logical operators and priority

foreword

In the continuous learning summary output, what I share today is the web front-end, JS basic arithmetic, assignment, unary, comparison, logical operators and priority

1. Arithmetic operators

Mathematical operators are also called arithmetic operators, mainly including addition, subtraction, multiplication, division, and remainder (modulus).
+: Sum
-: Difference
* : Product
/: Quotient
%: Modulo (remainder)
is often used as whether a certain number is divisible in development

The priority order of arithmetic operator execution
When using multiple operators to write programs at the same time, they will be executed in a certain order, which we call priority. In JavaScript, the higher the priority, the earlier it will be executed. If the priority is the same, it will be executed from left to right.
The priority of multiplication, division, and remainder is the same
. The priority of addition and subtraction is the same. The
priority of multiplication, division, and remainder is higher than that of addition and subtraction.
Using () can increase the priority
. ~~

<script>
        console.log(4 / 2)
        console.log(4 % 2)
        console.log(2 % 4)
        console.log(5 % 8)
    </script>

operation result:
Please add a picture description

2. Assignment operator

Assignment operator:
operator for assigning values ​​to variables Already learned assignment operator: = assigns the value on the right side of the equal sign to the left, requiring that the left side must be a container
Other assignment operators:
+=
-=
*=
/=
% =

Use these operators for quick operations when assigning values ​​to variables

+= assignment operator for example

	<script>
       let num = 18
       num = num + 1
       console.log(num)   // 结果是 19
    </script>

abbreviation

	<script>
       let num = 18
       num +=  1 
       console.log(num)  // 结果是 19
    </script>

3. Unary operator

Numerous JavaScript operators can be divided into unary operators, binary operators, and ternary operators according to the number of expressions required
Binary operators:

	<script>
	let num = 18 + 20
    </script>

Unary operators:

	<script>
		++num    等价于  num += 1
       num++
       console.log(num)
    </script>

We can have a simpler way of writing~~~

Auto-increment:
symbol: ++
makes the value of the variable +1

Decrement:
Sign: –
Let the value of the variable be -1

Usage scenario:
often used for counting. For example, if you perform 10 operations, use it to calculate how many times you have performed

Usage of the auto-increment operator:

  1. There is no difference between pre-increment and post-increment when used independently!
  2. In general development, we use it independently
  3. Later, i++ post-increment will use relatively more

4. Comparison operators

Compare the size of two data, whether they are equal

Comparison operators:

: Whether the left side is greater than the right side
<: Whether the left side is smaller than the right side
=: Whether the left side is greater than or equal to the right side
<=: Whether the left side is less than or equal to the right side
==: Whether the left and right sides are equal
=: Whether the type and value of the left and right sides are equal
!
: Whether the left and right sides are not equal
The comparison result is boolean type, that is, only true or false will be obtained

String comparison is the ASCII code corresponding to the character to be compared.
Compare from left to right.
If the first character is the same, then compare the second character, and so on
. There are few comparisons, just understand

NaN is not equal to any value, including itself

Try not to compare decimals, because decimals have precision issues

Implicit conversion will occur in the comparison between different types,
and finally the data will be implicitly converted into a number type for comparison.
Therefore, in development, if we make an accurate comparison, we prefer === or !==

5. Logical operators

Logical operators are used to solve multiple conditional judgments

symbol name daily reading features formula
&& logic and and The result is true only when both sides of the symbol are true one false is false
logical or or If there is a true on both sides of the symbol, it is true true is true
! logical NOT reconciliation true to false false to true true to false, false to true

|| Logical OR

Short-circuiting in logical operators

Short circuit:
only exists in && and ||, when a certain condition is met, the code on the right will not be executed.
Please add a picture description
Reason:
The result of the entire formula can be obtained through the left, so there is no need to judge the right

Operation result:
Regardless of && or ||, the operation result is the last executed expression value, which is generally used in variable assignment

	<script>
		console.log(false && 20)  //false
        console.log(5 < 3 && 20)  //false
        console.log(undefined && 20) //undefined
        console.log(null && 20) //null
        console.log(0 && 20) // 0
        console.log(10 && 20) // 20
    </script>
	<script>
		console.log(false || 20) // 20
        console.log(5 < 3 || 20) // 20
        console.log(undefined || 20) // 20
        console.log(null || 20) // 20
        console.log(0 || 20) // 20
        console.log(10 || 20) // 10
    </script>

6. Operator precedence

priority operator order
1 Parentheses ()
2 unary operator ++ - - !
3 arithmetic operator First * / % followed by + -
4 relational operator > >= < <=
5 equality operator == != === !==
6 Logical Operators first && after
7 assignment operator =
8 comma operator ,

first && after ||

The logical non-priority in the unary operator is very high
, and the logical and has a higher priority than the logical or

	<script>
	let a = 3 > 5 && 2 < 7 && 3 == 4 
        console.log(a);   
        //false ,此时发生了逻辑中断

        let b = 3 <= 4 || 3 > 1 || 3 != 2 
        console.log(b);  
        //true,此时发生了逻辑中断


        let c = 2 === "2"
        console.log(c);
         //false ,数据类型不匹配

        let d = !c || b && a 
        console.log(d); 
         //true,此时发生了逻辑中断
    </script>

7. Summary

insert image description here

Finally share a sentence:

Be constructive in what you say, and keep silent if you have nothing to say.
"A World Without Complaints"

That's all for this sharing! ! !

Welcome to leave a message to discuss in the comment area! !

Guess you like

Origin blog.csdn.net/qq_37255976/article/details/125264410