首页 > web前端 > js教程 > 正文

JS 操作符整理[推荐收藏]_基础知识

php中文网
发布: 2016-05-16 17:59:18
原创
1623人浏览过

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
>= is greater than or equal to
大于等于
5>=8 returns false
is less than or equal to
小于等于
5

Logical Operators
逻辑运算符

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

(x 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衔接起来了)

JoinMC智能客服
JoinMC智能客服

JoinMC智能客服,帮您熬夜加班,7X24小时全天候智能回复用户消息,自动维护媒体主页,全平台渠道集成管理,电商物流平台一键绑定,让您出海轻松无忧!

JoinMC智能客服 193
查看详情 JoinMC智能客服

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"

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号