zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JS操作符整理[推荐收藏]

JS 收藏 推荐 整理 操作符
2023-06-13 09:14:31 时间

ArithmeticOperators
算术运算符

Operator Description Example Result + Addition
x=2
y=2
x+y 4 - Subtraction
x=5
y=2
x-y 3 * Multiplication
x=5
y=4
x*y 20 / Division
15/5
5/2 3
2.5 % Modulus(divisionremainder)
余数 5%2
10%8
10%2 1
2
0 ++ Increment
递增 x=5
x++ x=6 -- Decrement
递减 x=5
x-- x=4

AssignmentOperators
赋值运算符

Operator Example IsTheSameAs = x=y x=y += 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

ComparisonOperators
比较(关系)运算符

Operator Description Example == isequalto
等于 5==8returnsfalse === isequalto(checksforbothvalueandtype)
等于(检查值和类型)*全吻合才算相等 x=5
y="5"

x==yreturnstrue
x===yreturnsfalse

!= isnotequal
不等于 5!=8returnstrue > isgreaterthan
大于 5>8returnsfalse < islessthan
小于 5<8returnstrue >= isgreaterthanorequalto
大于等于 5>=8returnsfalse <= islessthanorequalto
小于等于 5<=8returnstrue

LogicalOperators
逻辑运算符

Operator Description Example && and
x=6
y=3

(x<10&&y>1)returnstrue

|| or
x=6
y=3

(x==5||y==5)returnsfalse

! not
x=6
y=3

!(x==y)returnstrue

StringOperator
串符(连接作用)

Astringismostoftentext,forexample"HelloWorld!".Tosticktwoormorestringvariablestogether,usethe+operator.
在文字当中使用的比较多,举例来说“HelloWorld!”要将两个或多个字符串变量衔接在一起的话就得使用+符号

txt1="Whatavery"
txt2="niceday!"

txt3=txt1+txt2 

Thevariabletxt3nowcontains"Whataveryniceday!".
txt3变量现在包含“Whataveryniceday!”(把1和2衔接起来了)

Toaddaspacebetweentwostringvariables,insertaspaceintotheexpression,ORinoneofthestrings.
要给两个字符串变量中间添加空格就得在表达式里插入空格,或在其中的一个加上(空格)

txt1="Whatavery"
txt2="niceday!"
txt3=txt1+""+txt2
or
txt1="Whatavery"
txt2="niceday!"
txt3=txt1+txt2

Thevariabletxt3nowcontains"Whataveryniceday!".
现在变量txt3为“Whataveryniceday!”

ConditionalOperator
条件运算符

JavaScriptalsocontainsaconditionaloperatorthatassignsavaluetoavariablebasedonsomecondition.
JS有根据条件不同给变量不同值的条件运算符

Syntax
语法

variablename=(condition)?value1:value2 

Example
例子

greeting=(visitor=="PRES")?"DearPresident":"Dear"

IfthevariablevisitorisequaltoPRES,thenputthestring"DearPresident"inthevariablenamedgreeting.IfthevariablevisitorisnotequaltoPRES,thenputthestring"Dear"intothevariablenamedgreeting.
如果变量visitor的值等于PRES那么greeting的值就为"DearPresident"。如果不为PRES那么greeting的值就为"Dear"