Menu Close

javascript 运算符优先级

运算符优先级值

括号中的表达式在表达式的其余部分之前计算,函数在结果用于表达式的其余部分之前执行:

优先级 运算符 描述 例子
18 ( ) 表达式分组 (100 + 50) * 3
17 . 属于成员 person.name
17 [] 属于成员 person[“name”]
17 ?. 可选链 ES2020 x ?. y
17 () 函数调用 myFunction()
17 new 带参的新建 new Date(“June 5,2022”)
16 new 不带参的新建 new Date()

增量运算符

后缀递增在前缀递增之前执行。

15 ++ 后缀递增 i++
15 后缀递减 i–
14 ++ 前缀递增 ++i
14 前缀递减 –i

NOT 运算符

14 ! 逻辑非 !(x==y)
14 ~ 按位非 ~x

一元运算符

14 + 一元加 +x
14 一元减 -x
14 typeof 数据类型 typeof x
14 void 评估空 void(0)
14 delete 属性删除 delete myCar.color

算术运算符

指数运算在乘法之前执行。

乘法和除法在加法和减法之前执行。

13 ** 指数运算 ES2016 10 ** 2
12 * 10 * 5
12 / 10 / 5
12 % 除法余数 10 % 5
11 + 10 + 5
11 10 – 5
11 + 串联 “Bill” + “Gates”

移位运算符

10 << 左移 x << 2
10 >> 右移(带符号) x >> 2
10 >>> 右移(无符号) x >>> 2

关系运算符

9 in 对象中的属性 “PI” in Math
9 instanceof 对象的实例 x instanceof Array

比较运算符

9 < 小于 x < y
9 <= 小于或等于 x <= y
9 > 大于 x > y
9 >= 大于或等于 x >= Array
8 == 等于 x == y
8 === 严格相等 x === y
8 != 不相等 x != y
8 !== 严格不相等 x !== y

位运算符

7 & 按位与 x & y
6 ^ 按位异或 x ^ y
5 | 按位或 x | y

逻辑运算符

4 && 逻辑与 x && y
3 || 逻辑或 x || y
3 ?? 空值合并 ES2020 x ?? y

条件(三元)运算符

2 ? : 条件 ? “yes” : “no”

赋值运算符

赋值在其他操作之后执行。

2 = 简单赋值 x = y
2 : 冒号赋值 x: 5
2 += 加法赋值 x += y
2 -= 减法赋值 x -= y
2 *= 乘法赋值 x *= y
2 **= 指数赋值 x **= y
2 /= 除法赋值 x /= y
2 %= 取余赋值 x %= y
2 <<= 左移赋值 x <<= y
2 >>= 右移赋值 x >>= y
2 >>>= 无符号右移 x >>>= y
2 &= 位与赋值 x &= y
2 |= 位或赋值 x |= y
2 ^= 位异或赋值 x ^= y
2 &&= 逻辑与赋值 x &= y
2 ||= 逻辑或赋值 x ||= y
2 => 箭头 x => y
2 yield 暂停 / 恢复 yield x
2 yield* 委托运算符 yield* x
2 展开运算符 … x
1 , 逗号 x , y

以下是英文运算符优先级

Precedence Associativity Individual operators Notes
18: grouping n/a Grouping
(x)
[1]
17: access and call left-to-right Member access
x.y
[2]
Optional chaining
x?.y
n/a Computed member access
x[y]
[3]
new with argument list
new x(y)
[4]
Function call
x(y)
import(x)
16: new n/a new without argument list
new x
15: postfix operators n/a Postfix increment
x++
[5]
Postfix decrement
x–
14: prefix operators n/a Prefix increment
++x
[6]
Prefix decrement
–x
Logical NOT
!x
Bitwise NOT
~x
Unary plus
+x
Unary negation
-x
typeof x
void x
delete x [7]
await x
13: exponentiation right-to-left Exponentiation
x ** y
[8]
12: multiplicative operators left-to-right Multiplication
x * y
Division
x / y
Remainder
x % y
11: additive operators left-to-right Addition
x + y
Subtraction
x – y
10: bitwise shift left-to-right Left shift
x << y
Right shift
x >> y
Unsigned right shift
x >>> y
9: relational operators left-to-right Less than
x < y
Less than or equal
x <= y
Greater than
x > y
Greater than or equal
x >= y
x in y
x instanceof y
8: equality operators left-to-right Equality
x == y
Inequality
x != y
Strict equality
x === y
Strict inequality
x !== y
7: bitwise AND left-to-right Bitwise AND
x & y
6: bitwise XOR left-to-right Bitwise XOR
x ^ y
5: bitwise OR left-to-right Bitwise OR
x | y
4: logical AND left-to-right Logical AND
x && y
3: logical OR, nullish coalescing left-to-right Logical OR
x || y
Nullish coalescing operator
x ?? y
[9]
2: assignment and miscellaneous right-to-left Assignment
x = y
[10]
Addition assignment
x += y
Subtraction assignment
x -= y
Exponentiation assignment
x **= y
Multiplication assignment
x *= y
Division assignment
x /= y
Remainder assignment
x %= y
Left shift assignment
x <<= y
Right shift assignment
x >>= y
Unsigned right shift assignment
x >>>= y
Bitwise AND assignment
x &= y
Bitwise XOR assignment
x ^= y
Bitwise OR assignment
x |= y
Logical AND assignment
x &&= y
Logical OR assignment
x ||= y
Nullish coalescing assignment
x ??= y
right-to-left Conditional (ternary) operator
x ? y : z
[11]
right-to-left Arrow
x => y
[12]
n/a yield x
yield* x
Spread
…x
[13]
1: comma left-to-right Comma operator
x, y

 

READ  JavaScript 比较运算符
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!
Posted in JavaScript 基础

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!