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

來源:互聯網
上載者:User

Arithmetic Operators
算術運算子

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 (division remainder)
餘數
5%2
10%8
10%2
1
2
0
++ Increment
遞增
x=5
x++
x=6
-- Decrement
遞減
x=5
x--
x=4
Assignment Operators
賦值運算子
Operator Example Is The Same As
= 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
Comparison Operators
比較(關係)運算子
Operator Description Example
== is equal to
等於
5==8 returns false
=== is equal to (checks for both value and type)
等於(檢查值和類型)*全吻合才算相等
x=5
y="5"

x==y returns true
x===y returns false

!= is not equal
不等於
5!=8 returns true
> is greater than
大於
5>8 returns false
< is less than
小於
5<8 returns true
>= is greater than or equal to
大於等於
5>=8 returns false
<= is less than or equal to
小於等於
5<=8 returns true
Logical Operators
邏輯運算子
Operator Description Example
&& and
x=6
y=3

(x < 10 && y > 1) returns true

|| or
x=6
y=3

(x==5 || y==5) returns false

! not
x=6
y=3

!(x==y) returns true

String Operator
串符(串連作用)

A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.
在文字當中使用的比較多,舉例來說“Hello World!”要將兩個或多個字串變數銜接在一起的話就得使用 + 符號

txt1="What a very"txt2="nice day!"txt3=txt1+txt2 

The variable txt3 now contains "What a verynice day!".
txt3變數現在包含“What a verynice day!”(把1和2銜接起來了)

To add a space between two string variables, insert a space into the expression, OR in one of the strings.
要給兩個字串變數中間添加空格就得在運算式裡插入空格,或在其中的一個加上(空格)

txt1="What a very"txt2="nice day!"txt3=txt1+" "+txt2
or
txt1="What a very "txt2="nice day!"txt3=txt1+txt2

The variable txt3 now contains "What a very nice day!".
現在變數txt3為“What a very nice day!”

Conditional Operator
條件運算子

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JS有根據條件不同給變數不同值的條件運算子

Syntax
文法
variablename=(condition)?value1:value2 
Example
例子
greeting=(visitor=="PRES")?"Dear President ":"Dear "

If the variable visitor is equal to PRES, then put the string "Dear President " in the variable named greeting. If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.
如果變數visitor的值等於PRES那麼greeting的值就為"Dear President "。如果不為PRES那麼greeting的值就為"Dear"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.