In JavaScript, common operators include arithmetic operators, comparison operators and logical operators.
Table 1 JavaScript common operators
算数运算符 | 说明 | 举例 | 结果 |
---|---|---|---|
= | 赋值运算符。将运算符右边变量的值赋给左边变量。 | x = 5 ; | - |
加号。将两个数据相加。 | y=1 2; | y=3 | |
- | 减号。将两个数据相减。 | z = x-y; | z=2 |
* | 乘号。将两个数据相乘。 | a=x*y; | a=15 |
/ | 除号。将两个数据相除。 | b=x/z; | b=2.5 |
% | 求余运算。求两个数据相除的余数。 | c=x%z; | c=1 |
自加。将操作数加1。 | m= x; | m=6 x=6 | |
-- | 自减。将操作数减1。 | n=--x; | n=5 x=5 |
比较运算符 | 说明 | 举例 | 结果 |
== | 相等。若两数据相等,返回 true,否则返回 false。 | boolean1=(x==5); | boolean1=true |
!= | 不相等。若两数据不相等,返回 true,否则返回 false。 | boolean2=(x!=5); | boolean2=false; |
> | 大于。若左边数据大于右边数据,返回 true,否则返回 false。 | boolean4=(x>y); | boolean4=true |
< | 小于。若左边数据小于右边数据,则返回布尔值true,否则返回false。 | boolean5=(xboolean5=false |
|
>= | 大于等于。若左边数据大于或等于右边数据,返回 true,否则返回 false。 | boolean6=(x>=y); | boolean6=true |
<= | 小于等于。若左边数据小于或等于右边数据,返回 true,否则返回 false。 | boolean7=(x<=y); | boolean7=false |
逻辑运算符 | 说明 | 举例 | 结果 |
&& | 逻辑与。如果符号两边的操作数为真,返回true,否则返回false。 | boolean_a=true&&false; | boolean_a=false |
|| | 逻辑或。如果符号两边的操作数为假,返回false,否则返回true。 | boolean_b=true||false; | boolean_b=true |
! | 逻辑非。如果符号右边的操作数为真,返回false,否则返回true。 | boolean_c=!true; | boolean_c=false |
" " can also be used to concatenate strings
The " " sign can not only add two data, but can also be used to connect strings.
For example:
Save and run the code to display My name is Tom ! I'm 22 !
In the above example, there are strings and values. When strings and numerical values are mixed, JavaScript will automatically determine the function of the " " sign, whether it is an addition operation or a concatenation of strings. If concatenating strings, the numeric value will also be converted to a string.
Discussion on self-increment ( ) and self-decrement (--)
It is worth noting that the self-increment ( ) and self-decrement (--) operators have different meanings when placed before and after the operand. Put it in front of the operand (front self-increment/front self-decrement), first add 1 (subtract 1) to the operand, and then perform the operation; place it after the operand (last self-increment/last self-decrement), perform the operation first, Then add 1 to the operand (decrease 1).
For example:
Display the value of y
Show the value of z
Display the value of m
Show the value of n
Save and run the code, click on the four pieces of text in sequence, and they will all display 6.
Analysis:
For y , the value of x (x=5) plus 1 becomes 6 , and then the value of x is passed to y .
For z , first pass the value of x (x=6) to z , then add 1 to x , so the value becomes 7.
For m , the value of x (x=7) minus 1 is 6, and the value of x is passed to m .
For n , first pass the value of x (x=6) to n , then subtract 1 from x , so the value becomes 5 .
Abbreviation for arithmetic operator
In order to facilitate operation and reduce code writing, JavaScript also supports the abbreviations of common mathematical operators.
Table 2 Abbreviations of common arithmetic operators
Operator | Example | Equivalent to | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
= | x =y | x=x y | ||||||||||||||||||
-= | x-=y | x=x-y | ||||||||||||||||||
*= | x*=y | x=x*y | ||||||||||||||||||
/= | x/=y | x=x/y | ||||||||||||||||||
%= | x%=y | x=x%y
|
The above is the entire content of this article, I hope you all like it.