判断符
1.空值合并运算符( ??)
当左侧结果为null或者undefined时返回右侧的值
const foo = null ?? 'default string'; console.log(foo); // expected output: "default string" const baz = 0 ?? 42; console.log(baz); // expected output: "0"
2. 可选链( ?. )
可选链是ES11中新增一个特性,主要作用是让我们的代码在进行null和undefined判断时更加清晰和简洁
const obj = {
friend: {
girlFriend: {
name: "lucy"
}
}
}
if (obj.friend && obj.friend.girlFriend) {
console.log(obj.friend.girlFriend.name)
}
3.逻辑或分配( ||= )
x ||= y
等同于
x || (x=y)
4.逻辑与分配( &&= )
x &&= y
等同于
x && (x=y)
计算符
1.方根计算( ** )
如果我们要计算一个数的几次方,可以 使用Math.pow(x,y)表示x的y次方。但是使用快捷操作符可以用x**y来表示
2.位运算符( ~ )
取反运算符,用以将数字的二进行取反,例如,~5将对数字5进行按位取反操作。在二进制表示中,5的二进制形式是00000101,取反后变为11111010,即-6的二进制表示。因此,~5的结果是-6。