JavaScript operators
JavaScript operators mainly include:
Arithmetic operators
Assignment operators
Comparison operators
Ternary operator
Logical operator
String concatenation operator
Arithmetic operators
DescriptionOperator##+ Add y = 2+1 y = 3
- Subtract y = 2-1 y = 1
##For pre-add and post-add, the result after execution is the variable plus 1. The difference is that the return result is different during execution. Please refer to the following two examples:
var x = 2;alert(++x); //Output: 3
alert(x); //Output: 3 var y = 2;alert(y++); / /Output: 2
alert(y); //Output: 3
##Assignment operator
Assignment operator = is used for assignment operations. The assignment operator is used to assign the value on the right to the variable on the left. Set y = 6, see the table below:
Exampleis equivalent to
Operation result
##= y = 6 empty y = 6+= y += 1 y = y+1 y = 7
-= y -= 1 y = y-1 y = 5
*= y *= 2 y = y*2 y = 12
/= y /= 2 y = y/2 y = 3
%= y %= 4 y = y%4 y = 2
##Comparison operations Symbol
OperatorExplanationExampleOperation result
== Equal to 2 == 3 FALSE
2 === "2"FALSE )
!= Not equal, it can also be written as <> 2 == 3 TRUE > Greater than 2 > 3 FALSE < Less than 2 < 3 TRUE >= Greater than or equal to 2 >= 3 FALSE <= Less than or equal to 2 < ;= 3 TRUEternary operator
Ternary operator can be regarded as a special comparison Operator: (expr1) ? (expr2) : (expr3)x = 2;y = (x == 2) ? x : 1;
alert(y); //Output: 2
Logical Operators
OperatorExplanationExampleOperation result
&& Logical AND (and) x = 2; y = 6; x && y > 5 FALSE
|| Logical OR (or) x = 2; y = 6; The opposite of x = 2; y = 6; !(x > y) TRUE
The connection operator + is mainly used to connect two strings or string variables. Therefore, when you use this operator on strings or string variables, you are not adding them.
Example:x = "beijing";
y = x + "Hello!"; //Result: y = "Hello beijing!"// To add spaces between two strings, you need to insert spaces into a string:
y = x + "Hello!"; //Result: y = "beijing you OK! "
When performing the concatenation (addition) operation on strings and numbers, the numbers will be converted into strings and then concatenated (added):
x = 25;
y = "I am this year" + x + "years old"; //Result: y = "I am 25 years old this year"php中文网(php.cn)