JS 操作符主要包括算术运算符, 赋值运算符, 比较(关系)运算符, 逻辑运算符, 串符(连接作用), 条件运算符等
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
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 |
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 |
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 |
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 |
A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.
在文字当中使用的比较多,举例来说 "Hello World!" 要将两个或多个字符串变量衔接在一起的话就得使用 + 符号
|
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.
要给两个字符串变量中间添加空格就得在表达式里插入空格,或在其中的一个加上(空格)
|
The variable txt3 now contains "What a very nice day!".
现在变量 txt3 为 "What a very nice day!"
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JS 有根据条件不同给变量不同值的条件运算符
|
|
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"
来源: