Operator, also known as operator, is a symbol used to implement functions such as assignment, comparison, and performing arithmetic operations.
Commonly used operators in JavaScript are:
concept: Symbols used in arithmetic operations, used to perform arithmetic operations on two variables or values.
Operator | Description | Example |
---|---|---|
+ | Add | 10+20=30 |
-Subtract | 20-10= | 10 |
* | Multiply | 10*20=200 |
/ | Divide | 10/20=0.5 |
% | Take the remainder (modulo) | and return the remainder of the division 9% 2=1 |
console. log(1 + 1); //2 console.log(1 - 1); //0 console.log(1 * 1); //1 console.log(1 / 1); //1 console.log(4 % 2); //0
floating point numbers will have errors in arithmetic operations (avoid direct participation in calculations):
console.log(0.1 + 0.2); //0.30000000000000004
cannot directly determine whether two floating point numbers are equal.
var num = 0.1 + 0.2; console.log(num == 0.3); //false
Expression and return value:
composed of numbers and operators A formula composed of , variables, etc. is called an expression.
The expression will eventually return a result to us, which is called the return value.
If you need to repeatedly add or subtract 1 to a numeric variable, you can use the increment ( ++
) and decrement ( --
) operators to accomplish this.
Complicated writing:
var num = 1; num = num + 1; num = num + 1; console.log(num); //3
++
is written in front of the variable
++num
prefix increment is to increase by 1, similar to num=num+1
var age = 10; ++age; console.log(age);//11 Similar to the usage formula of age = age + 1
: add it first, then return the value
console.log(age); var a = 10; console.log(++a + 10); //(10+1)+10=21
++
is written after the variable
num++
post-increment, which means adding 1 to itself, similar to num=num+1
var age = 10; age++; console.log(age);//11 is similar to
the usage formula of age = age + 1: return the original value first, then add
var a = 10; console.log(a++ + 10); //10+10=20 console.log(a); //11
num++;
Exercise:
var e = 10; var f = e++ + ++e; //1.e++=10 e=11 2.++e=12 f=10+12 console.log(f); //22
concept: Comparison operator (relational operator) is an operator used when comparing two data . After comparison operation, a Boolean value (true/false) will be returned. As the result of a comparison operation.
Operator name | description | case | result |
---|---|---|---|
< | less than sign | 1>2 | true |
> | greater than sign | 1>2 | false |
>= | greater than or equal to sign (greater than or equal to) | 2>=2 | true |
<= | less than or equal to sign (less than or equal to) | 3<=2 | false |
== | Equality sign (will transform) | 17==17 | true |
!= | inequality sign | 17!=17 | false |
=== !== | Congruent, the value and data type must be consistent | 17==='17' | false |
console.log( 2 <= 5); //true console.log('Yue Zeyi' = 'personal blog'); //false console.log(17 == '17'); //true default conversion data type, string type is converted to numeric type console.log(17 = '17'); //false data type is different, value and data type are requiredUsage of
consistent
symbols | = | assignment |
---|---|---|
, | assign | the right side to the left side |
== | Determine | whether the values on both sides are equal (there is implicit conversion) |
=== | Congruence | Determine whether the values and data types on both sides are exactly the same |
concept : Logical operators are used Operators that perform Boolean operations also return a Boolean value. It is often used to judge multiple conditions in later development.
Logical operator | description | example |
---|---|---|
&& | "logical AND", referred to as "AND" and | ture && false |
丨丨 | "logical or", referred to as "OR" or | ture丨丨 false |
! | "logical NOT", referred to as "not" not | ! true |
Symbol: &&
is true when both sides of and
true
true
. As long as one side is false
, the result is false
console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true
symbol: ||
is equivalent to if
both sides of or are false
, the result is false
, as long as one side is true
, the result is true
console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true
symbol: !
Relative to not,
logical negation is also called the negation symbol, which is used to obtain the opposite value of a Boolean value.
console.log(!true); //false console.log(!false); //true
The principle of short-circuit operation: when there are multiple expressions (values), when the expression value on the left can determine the result, the expression on the right will no longer continue to be operated. The value of the formula.
Logical AND:
表达式1 && 表达式2
console.log(123 && 456); //Returns 456, all numbers except 0 are true. console.log(123 && 456 && 789); //Return 789, push back in sequence console.log(0 && 456); //0
logical OR:
表达式1 || 表达式2
console.log(123 || 456); //123 console.log(123 || 456 || 123 + 456); //123 console.log(0 || 456 || 123 + 456); //456
Note: Logical interruption will cause a short-circuit operation, that is, the subsequent code will not be executed, affecting the programmer's running results.
varnum = 0; console.log(123 || num++); //Logical interruption caused num++ not to be executed console.log(num); //0
concept : operator used to assign data to variables Assignment
operator | description | case |
---|---|---|
= | direct Assign | var name='Yue Zeyi'; |
+=, -= | add or subtract a number before assigning | var age=10; age+=5; //15 |
*=, /=, %= | after multiplication, division and remainder Then assign | var age=10; age*=5; //10 |
var num = 5; num += 10; console.log(num); //5+10=15 num *= 3; console.log(num); //15*3=45
operator | order | 1 |
---|---|---|
parentheses | ( | () |
2 | unary operator | ++ -- ! |
3 | arithmetic operator | first * / then + - |
4 | relational operation Symbols | > >= < <= |
5 | Equality operator | == != === !== |
6 | Logical operator | && followed by丨丨 |
7 | Assignment operator | = |
8 | Comma operator | , |
console.log(4 >= 6 || 'I' != 'you' && !(12 * 2 == 144) && true); //true /* Logical operators are divided into four sections 1.4 >= 6 to get false 2.'I' != 'you' must be true 3.!(12 * 2 == 144) gets true 4.true Then judge the logical AND: 2 and 3 are true, 3 and 4 are true Then judge the logical or: true */