赋值运算符
名字 | 简写的运算符 | 含义 |
赋值 | 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 |
求幂赋值 | 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 |
按位异或赋值 | x ^= y | x = x ^ y |
按位或赋值 | `x | = y` |
逻辑与赋值 | x &&= f() | x && (x = f()) |
逻辑或赋值 | `x | |
逻辑空赋值 | x ??= f() | x ?? (x = f()) |
比较运算符
运算符 | 描述 | 返回true的示例 |
等于 Equal(en-US) (== ) | 如果两边操作数相等时返回 true 。 | 3 == var1 "3" == var1 3 == '3' |
不等于 Not equal(en-US) (!= ) | 如果两边操作数不相等时返回 true | var1 != 4 var2 != "3" |
全等 Strict equal(en-US) (=== ) | 两边操作数相等且类型相同时返回 true 。参见 Object.is and sameness in JS. | 3 === var1 |
不全等 Strict not equal(en-US) (!== ) | 两边操作数不相等或类型不同时返回 true 。 | var1 !== "3" 3 !== '3' |
大于 Greater than(en-US) (> ) | 左边的操作数大于右边的操作数返回 true | var2 > var1 "12" > 2 |
大于等于 Greater than or equal(en-US) (>= ) | 左边的操作数大于或等于右边的操作数返回 true | var2 >= var1 var1 >= 3 |
小于 Less than(en-US) (< ) | 左边的操作数小于右边的操作数返回 true | var1 < var2 "2" < 12 |
小于等于 Less than or equal(en-US) (<= ) | 左边的操作数小于或等于右边的操作数返回 true | var1 <= var2 var2 <= 5 |
备注:(=>)不是运算符,而是箭头函数的标记符号
算数运算符
运算符 | 描述 | 示例 |
求余(% ) | 二元运算符。返回相除之后的余数。 | 12 % 5 返回 2。 |
自增(++ ) | 一元运算符。将操作数的值加一。如果放在操作数前面(++x ),则返回加一后的值;如果放在操作数后面(x++ ),则返回操作数原值,然后再将操作数加一。 | var x=3;console.log(++x); //4 console.log(x); //4 var y=3; console.log(y++); //3 console.log(y); //4 |
自减(-- ) | 一元运算符。将操作数的值减一。前后缀两种用法的返回值类似自增运算符。 | var x=3; console.log(--x); //输入 2,x=2var y=3;console.log(y--);//输出 3,x=2; |
一元负值符(- ) | 一元运算符,返回操作数的负值。 | var x=3; console.log(-x); //输入 -3 |
一元正值符(+ ) | 一元运算符,如果操作数在之前不是数值,试图将其转换为数值。 | console.log( +'3' ); // 3 console.log( '3' ); // '3' console.log(+true); // 1 |
指数运算符(\*\* ) | 计算底数(base )的指数(exponent )次方,表示为 base^exponent 。 | 2 \*\* 3 返回 8 。10 \*\* -1 返回 0.1 。 |
位运算符
Operator | Usage | Description |
按位与 AND(en-US) | a & b | 在 a,b 的位表示中,每一个对应的位都为 1 则返回 1,否则返回 0. |
按位或 OR(en-US) | `a | b` |
按位异或 XOR(en-US) | a ^ b | 在 a,b 的位表示中,每一个对应的位,两个不相同则返回 1,相同则返回 0. |
按位非 NOT(en-US) | ~ a | 反转被操作数的位。 |
左移 shift(en-US) | a << b | 将 a 的二进制串向左移动 b 位,右边移入 0. |
算术右移 | a >> b | 把 a 的二进制表示向右移动 b 位,丢弃被移出的所有位。(译注:算术右移左边空出的位是根据最高位是 0 和 1 来进行填充的) |
无符号右移 (左边空出位用 0 填充) | a >>> b | 把 a 的二进制表示向右移动 b 位,丢弃被移出的所有位,并把左边空出的位都填充为 0 |
逻辑运算符
运算符 | 范例 | 描述 |
逻辑与(en-US) (&& ) | expr1 && expr2 | (逻辑与) 如果 expr1 能被转换为 false,那么返回 expr1;否则,返回expr2。因此,&& 用于布尔值时,当操作数都为 true 时返回 true;否则返回 false. |
逻辑或(en-US) (` | `) | |
逻辑非(en-US) (! ) | !expr | (逻辑非) 如果操作数能够转换为 true 则返回 false;否则返回 true。 |
短路求值
作为逻辑表达式进行求值是从左到右,它们是为可能的“短路”的出现而使用以下规则进行测试:
false
&& anything // 被短路求值为 falsetrue
|| anything // 被短路求值为 true
字符串运算符
连接操作符(+)连接两个字符串值相连接,返回另一个字符串,它是两个操作数串的结合。
console.log("my " + "string"); // console logs the string "my string".
console.log("my " + "string"); // console logs the string "my string".
条件(三元)运算符
条件运算符是 JavaScript 中唯一需要三个操作数的运算符。运算的结果根据给定条件在两个值中取其一。语法为:条件 ? 值 1 : 值 2
。如果条件为真,则结果取值1
,否则为值2
。
var status = age >= 18 ? "adult" : "minor";
// age >= 18 status => adult
// age < 18 status => minor
逗号运算符
逗号(,
)运算符对它的每个操作数从左到右求值,并返回最后一个操作数的值。
let x = 1
x = (x++, x)
console.log(x) // Expected output: 2
x = (2, 3)
console.log(x) // Expected output: 3
一元运算符
delete
delete操作符,删除一个对象的属性或者一个数组中某一个键值。案例如下:
delete objectName.property;
delete objectName[index];
delete property; // legal only within a with statement
- 能使用
delete
删除各种各样的的隐式声明,但是被var
声明的除外。 - 如果
delete
操作成功,属性或元素会变成undefined
。如果delete
可行返回true
,反之返回false
。 - 删除数组中的元素时,数组长度是不变的,数组元素值变为
undefined
。
typeof
typeof 操作符 可通过下面 2 种方式使用:
typeof operand;
typeof (operand);
typeof
操作符返回一个表示 operand
类型的字符串值。operand
可为字符串、变量、关键词或对象,其类型将被返回。operand
两侧的括号为可选。
void
void
运算符运用方法如下:
void expression;
void (expression);
void
运算符,表明一个运算没有返回值。expression
是 javaScript 表达式,括号中的表达式是一个可选项,当然使用该方式是一种好的形式。
关系运算符
in
in操作符,如果所指定的属性确实存在于所指定的对象中,则会返回true
,语法如下:
propNameOrNumber in objectName;
在这里propNameOrNumber
可以是一个代表着属性名的字符串或者是一个代表着数组索引的数值表达式,而objectName
则是一个对象名。
// Arrays
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees; // returns true
3 in trees; // returns true
6 in trees; // returns false
"bay" in trees; // returns false (you must specify the index number,
// not the value at that index)
"length" in trees; // returns true (length is an Array property)
// Predefined objects
"PI" in Math; // returns true
var myString = new String("coral");
"length" in myString; // returns true
// Custom objects
var mycar = { make: "Honda", model: "Accord", year: 1998 };
"make" in mycar; // returns true
"model" in mycar; // returns true
instanceof
如果所判别的对象确实是所指定的类型,则返回true
。其语法如下:
objectName instanceof objectType;
可选链运算符 (?.)
可选链运算符(?.
)允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。类似于.
链式运算符,但在引用为空(nullish) (null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值是undefined
。
obj.val?.prop
obj.val?.[expr]
obj.func?.(args)
短路计算:
当在表达式中使用可选链时,如果做操作数是null
或undefined
,表达式不会被计算。
let potentiallyNullObj = null;
let x = 0;
let prop = potentiallyNullObj?.[x++];
console.log(x); // x 将不会被递增,依旧输出 0
空值合并运算符(??)
空值合并运算符(??
)是一个逻辑运算符,当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数。
const nullValue = null;
const emptyText = ""; // 空字符串,是一个假值,Boolean("") === false
const someNumber = 42;
const valA = nullValue ?? "valA 的默认值";
const valB = emptyText ?? "valB 的默认值";
const valC = someNumber ?? 0;
console.log(valA); // "valA 的默认值"
console.log(valB); // ""(空字符串虽然是假值,但不是 null 或者 undefined)
console.log(valC); // 42
逻辑空赋值(??=)
逻辑空赋值运算符(x ??= y
)仅在 x 是空值(null
或 undefined
)时对其赋值。
逻辑空赋值的语法短路也意味着x ??= y
等价于x ?? (x = y);
运算符优先级
Operator type | Individual operators |
member | . [] |
call / create instance | () new |
negation/increment | ! ~ - + ++ -- typeof void delete |
multiply/divide | \* / % |
addition/subtraction | + - |
bitwise shift | << >> >>> |
relational | < <= > >= in instanceof |
equality | == != === !== |
bitwise-and | & |
bitwise-xor | ^ |
bitwise-or | ` |
logical-and | && |
logical-or | ` |
conditional | ?: |
assignment | = += -= \*= /= %= <<= >>= >>>= &= ^= ` |
comma | , |