Operator
Operator is also called Operator , A symbol used to realize assignment, comparison and execution of arithmetic operations and other functions
Commonly used operators in JavaScript
- Arithmetic Operator
- Increment and decrement operators
- Comparison operator
- Logical Operators
- Assignment operator
Arithmetic Operator
Operator | description | Instance |
---|---|---|
+ | plus | 10+20=30 |
- | Less | 10-20=-10 |
* | Multiply | 10*20=200 |
/ | except | 10/20=0.5 |
% | Take the remainder (modulo) | 9%2=1 |
Floating point precision problem
The highest precision of floating-point numbers is 17 digits, but its precision is far inferior to integers when performing arithmetic calculations.
var result=0.1+0.2;//0.30000000000000004
console.log(result);
console.log(0.07*100);//7.000000000000001
We cannot directly judge whether two floating-point numbers are equal
Increment and decrement operators
If you need to repeatedly add or subtract 1 to a numeric variable, you can use the ++ and – operators to complete.
They can be placed in the front or in the back,
we can bePre-increment operatorwithPost-increment operator
The difference is: add it first or return the value first
var e=10;
f=e++ + ++e;
console.log(f);//22
Comparison operator
It is the operator used when comparing two data. After the comparison operation, a Boolean value will be returned as the result of the comparison operation
Operator name | Description | Case study | result |
---|---|---|---|
< | Less than | 1<2 | true |
> | more than the | 1>2 | false |
>= | greater or equal to | 2>=2 | true |
<= | Less than or equal to | 3<=2 | false |
== | Sentence waits, will transform | 37==37 | true |
!= | Unequal | 37!=37 | false |
=== !== | Congruent requirement values and data types are always | 37===‘37’ | false |
Logical Operators
Operator used to perform Boolean operations, and its return value is also a Boolean value
Logical Operators | Description | Case study |
---|---|---|
&& | versus | true&true |
|| | or | true||true |
! | non- | !true |
Short-circuit operation (logical interrupt)
When there are multiple expressions, when the value of the expression next to it can determine the result, the value of the expression on the right is no longer calculated;
console.log(123||456);//123
console.log(0||456);//456
console.log(123||456||789);//123
Take the logical OR as an example. If the
first one is true, then the first value will be returned. If the
first one is false, then the second value will be returned.
Assignment operator
Operators used to assign data to variables
Assignment operator | Description | Case study |
---|---|---|
= | Direct assignment | var username=‘41’ |
+= -= | Assign after adding or subtracting a number | var age = 10; age + = 5; |
*= /= %= | Assign after multiplying and dividing modulo | var age = 2; age * = 5; |
Operator precedence
priority | Operator | order |
---|---|---|
1 | Parentheses | () |
2 | Unary operator | ++ – ! |
3 | Arithmetic Operator | First* /% then +- |
4 | Relational operator | > >= < <= |
5 | Equality operator | == != === !== |
6 | Logical Operators | First && then || |
7 | Assignment operator | = |
8 | Comma operator | , |
Exercise
console.log(4>=6||'peo'!='41'&&!(12*2==144)&&true)//true
var num=10;
console.log(5==num/2&&(2+2*num).toString()==='22');//true