JS 操作符整理[推薦收藏]
Arithmetic Operators
算術(shù)運(yùn)算符
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) 余數(shù) |
5%2 10%8 10%2 |
1 2 0 |
++ | Increment 遞增 |
x=5 x++ |
x=6 |
-- | Decrement 遞減 |
x=5 x-- |
x=4 |
Assignment Operators
賦值運(yùn)算符
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
比較(關(guān)系)運(yùn)算符
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 |
!= | 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
邏輯運(yùn)算符
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.
在文字當(dāng)中使用的比較多,舉例來說“Hello World!”要將兩個(gè)或多個(gè)字符串變量銜接在一起的話就得使用 + 符號(hào)
txt1="What a very" txt2="nice day!" txt3=txt1+txt2 |
The variable txt3 now contains "What a verynice day!".
txt3變量現(xiàn)在包含“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.
要給兩個(gè)字符串變量中間添加空格就得在表達(dá)式里插入空格,或在其中的一個(gè)加上(空格)
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!".
現(xiàn)在變量txt3為“What a very nice day!”
Conditional Operator
條件運(yùn)算符
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JS有根據(jù)條件不同給變量不同值的條件運(yùn)算符
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"
相關(guān)文章
JavaScript數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹詳解
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹詳解,本文講解了二叉樹的概念、二叉樹的特點(diǎn)、二叉樹節(jié)點(diǎn)的定義、查找最大和最小值等內(nèi)容,需要的朋友可以參考下2015-02-02每日十條JavaScript經(jīng)驗(yàn)技巧(二)
本文是每日十條JavaScript經(jīng)驗(yàn)技巧系列文章的第二篇,同樣給大家匯總介紹10條個(gè)人在項(xiàng)目中的一些經(jīng)驗(yàn),分享給大家,希望大家能夠喜歡2016-06-06Javascript實(shí)例教程(19) 使用HoTMetal(7)
Javascript實(shí)例教程(19) 使用HoTMetal(7)...2006-12-12javascript事件綁定學(xué)習(xí)要點(diǎn)
這篇文章主要介紹了javascript事件綁定學(xué)習(xí)要點(diǎn),主要包含下面四個(gè)方面1.傳統(tǒng)事件綁定的問題,2.W3C事件處理函數(shù),3.IE事件處理函數(shù),4.事件對(duì)象的其他補(bǔ)充,有需要的小伙伴可以參考下2016-03-03以Python代碼實(shí)例展示kNN算法的實(shí)際運(yùn)用
這篇文章主要介紹了以Python代碼實(shí)例展示kNN算法的實(shí)際運(yùn)用,這里舉了一個(gè)用來預(yù)測豆瓣電影用戶的性別的例子,需要的朋友可以參考下2015-10-10javascript學(xué)習(xí)筆記(二) js一些基本概念
javascript學(xué)習(xí)筆記之js一些基本概念,學(xué)習(xí)js的朋友可以參考下2012-06-06Javascript核心讀書有感之表達(dá)式和運(yùn)算符
這篇文章主要介紹了Javascript核心讀書有感之表達(dá)式和運(yùn)算符,十分詳細(xì),需要的朋友可以參考下2015-02-02在JavaScript中操作時(shí)間之setYear()方法的使用
這篇文章主要介紹了在JavaScript中操作時(shí)間之setYear()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06