当前位置:网站首页>understand js operators
understand js operators
2022-07-31 05:59:00 【messy me】
目录
The precedence of each operator
运算符也叫操作符,通过运算符可以对一个或多个值进行运算,and get the result of the operation.比如:typeof就是一种运算符,可以获得一个值的类型.
算数运算符
算术运算符:
When operating on non-numeric values,First convert it to a numeric type for calculation.
and any value with NaNDo the calculation will getNaN.
- +
+可以对两个值进行加法运算,并将结果返回.如:a=1+2;
如果对两个字符串进行加法运算,will be concatenated.如:a="abc"+"d";a="abcd"
And any value is added to the string,都会先转换为字符串,然后再进行运算.
This transformation uses the calling functionString()Type conversions are performed on numeric values of the remaining types.
- -
-可以对两个值进行减法运算,并将结果返回.如:a=2-1;
- *
*可以对两个值进行乘法运算,并将结果返回.如: a=2*1;
- /
/可以对两个值进行除法运算,并将结果返回.如:a=2/1;
任何值做- * /will be converted to Number
我们可以利用这一特点做隐式的类型转换
可以通过为一个值-0 *1 /1来将其转换为Number,原理和Number()函数一样.
- %
%A modulo operation can be performed on a value(取余数).如:a=6%2;
一元运算符
一元运算符:
- 一元运算符,只需要一个操作数
+ 正号
- 正号不会对数字产生任何影响
- 负号
- 负号可以对数字进行负号的取反
- 对于非Number类型数据,
它会先将其转换为Number,然后再运算.可以对一个其他数据类型使用+,将其转换为number
它的原理和Number()函数一样.
自增与自减
自增与自减:
- 自增 ++
- 通过自增可以使变量在自身的基础上增加1,对于一个变量自增后,The value of the original variable will be incremented immediately1.
- 自增分为两种:后加加(++)and front gaga(++).Both methods can add one to the value of the variable.
不同的区别是 ++a 与 a++ 的值不同
a++的值等于原变量的值 (自增前的值)a++ = a
++a的值等于原变量的新值 (自增后的值) b = a+1;++a = b;
- 自减 --
- 通过自减可以使变量在自身的基础上减少1,After a variable is decremented,The value of the original variable will be decremented immediately1.
- 自减分为两种:后减减(--)and minus minus before(--).Both methods can subtract one from the value of the variable.
不同的区别是 --a 与 a-- 的值不同
a--的值等于原变量的值 (自减前的值)a-- = a
--a的值等于原变量的新值 (自减后的值) b = a-1;--a = b;
逻辑运算符
逻辑运算符:
js中为我们提供了三种逻辑运算符:
- ! 非
!Can be used to logically NOT a value
所谓非运算就是指对一个布尔值进行取反操作,
true变false,false变为true.如a=false,!a=true
If negate a value twice with NOT,它不会变化.If operating on a non-boolean value,
则会将其转换为布尔值,然后再取反.We can use this feature to convert non-boolean values to boolean values.
- && 与
&&You can AND the values on both sides of a symbol and return the result.
两个值中只要有一个值为false就返回false,只有两个值都为true时,才会返回true.
jsof and yes“短路的与”,when the first value is false时,则不会看第二个值.
- || 或
||You can logically OR the values on both sides of a symbol and return the result
Only one of the two values is true就返回true,只有两个值都为false时,才会返回false.
js中的“或”属于短路的或
如果第一个值为true,then it is not looking at the second value.
&& || 非布尔值的情况
- When performing an AND-OR operation on non-boolean data,会先将其转换为布尔值,然后再运算,并且返回原值
与运算:
- 如果第一个值为true,则必然返回第二个值
- 如果第一个值为false,则直接返回第一个值
或运算:
- 如果第一个值为true,则直接返回第一个值
- 如果第一个值为false,则直接返回第二个值
赋值运算符
赋值运算符:
=
可以将符号右侧的值赋值给符号左侧的变量.如a=1;a="字符串";a=true;
+=
a += b 等价于 a = a + b;
-=
a -= b 等价于 a = a - b;
*=
a *= b 等价于 a = a * b;
/=
a /= b 等价于 a = a / b;
%=
a %= b 等价于 a = a % b;
The code on both sides achieves the same effect,Choose by yourself in actual use.
关系运算符
关系运算符:
通过关系运算符可以比较两个值之间的大小关系,如果关系成立返回true,否则返回false.
- > 大于号
判断符号左侧的值是否大于右侧的,如果成立,返回true.否则返回false.
- < 小于号
Determines whether the value on the left side of the symbol is less than the value on the right side,如果成立,返回true.否则返回false.
- >= 大于等于号
Determines whether the value on the left side of the symbol is greater than or equal to the value on the right side,如果成立,返回true.否则返回false.
- <= 小于等于号
Determines whether the value on the left of the symbol is less than or equal to the value on the right,如果成立,返回true.否则返回false.
非数值的情况
- 对于非数值进行比较时,It will be converted to a number for comparison.
- 任何值与NaN做任何比较都是false
- 如果两侧的值都是字符串时,不会将其转换为数字进行比较,Instead, the character encodings of the strings at both ends are compared separately
比较字符编码时是一位一位进行比较,If the two numbers are the same,则比较下一位.
- When comparing string-type numbers,Be sure to perform data type conversion first.
相等运算符
相等运算符:
The equality operator is used to compare two values for equality,如果相等返回true,否则返回false.
- 使用 == 来做相等运算
- 当使用==for equality comparisons,如果两个值的类型不同,则会自动进行类型转换.
Convert it to the same data type,data comparison.
- undefined与nullvalue when comparing data,会返回true
- NaN不和任何值相等,包括其本身.
!= The inequality operator is used to determine whether two values are not equal,如果不相等返回true,否则返回false.
- 使用 != 来做不相等运算
- 不相等运算符也会对变量进行自动的类型转换,然后再进行比较.
=== 全等运算符
- Used to determine whether two values are congruent,它和相等类似,The difference is that it doesn't just judge two values of data.
If the types of the two values are not equal,直接返回false.If the two data memory addresses are different,也会返回false.
!== 不全等运算符
- Used to determine whether two values are not equal,It is the same as the congruence judgment mechanism.
条件运算符(三元运算符)
条件运算符(三元运算符):
如:“条件表达式?语句1:语句2;”
条件运算符在执行时,首先对条件表达式进行求值.
如果该值为true,则执行语句1,并返回执行结果.如果该值为false,则执行语句2.
如果条件的表达式的求值结果是一个非布尔值,The numerical conversion will be performed first,然后再进行运算.
The precedence of each operator
1 | . 、[] 、new |
2 | () |
3 | ++、-- |
4 | !、~、+(单目)、-(单目)、typeof、void、delete |
5 | %、*、/ |
6 | +(双目)、-(双目) |
7 | <<、>>、>>> |
8 | <、<=、>、>= |
9 | ==、!==、=== |
10 | & |
11 | ^ |
12 | | |
13 | && |
14 | || |
15 | ?: |
16 | =、+=、-=、*=、/=、%=、<<=、>>=、>>>=、&=、^=、|= |
17 | , |
边栏推荐
- 带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
- 【windows】--- SQL Server 2008 超详细安装教程
- For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
- [Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
- mac10.14中安装mysqldb
- The latest MySql installation teaching, very detailed
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- Pure shell implementation of text replacement
- Artifact SSMwar exploded Error deploying artifact.See server log for details
- Attribute Changer的几种形态
猜你喜欢
What is the difference between NFT and digital collection?
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
Yuan prospect and four track of the universe
MySql创建数据表
Build DVWA with phpstudy
DeFi 项目中的治理Token
Getting to know regular expressions
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
Access database query
随机推荐
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
数据库 | SQL查询进阶语法
Android software security and reverse analysis reading notes
计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
Linux修改MySQL数据库密码
小米手机短信定位服务激活失败
cocos2d-x-3.2 不能混合颜色修改
"limit" query in Oracle database
podspec 校验依赖出错问题 pod lib lint ,需要指定源
MySql创建数据表
【云原生】微服务Nacos的简单介绍与使用
Several forms of Attribute Changer
cocos2d-x-3.x 修改和纪录
How to distinguish big and small endian in C language
腾讯云轻量服务器删除所有防火墙规则
js中的全局作用域与函数作用域
On the side of Ali, tell me what are the application scenarios of message middleware you know?
cocos2d-x-3.2 Physics
js中的函数
Powershell中UTF-8环境中文乱码解决办法