# 空值合并运算符
空值合并操作符(??)是一个逻辑操作符,当左侧的操作数为 null (opens new window) 或者 undefined (opens new window) 时,返回其右侧操作数,否则返回左侧操作数。
逻辑与 (opens new window)(&&):(逻辑与) 如果expr1能被转换为false,那么返回expr1;否则,返回expr2。
逻辑或 (opens new window)(||): (逻辑或) 如果expr1能被转换为true,那么返回expr1;否则,返回expr2。
如果要排除 null 和 undefined ,用 ??
如果要排除 new Boolean() = false ( null、undefined、0、''、false、NaN ) 的情况 ,用 ||
new Boolean(null) // false
new Boolean(undefined) // false
1
2
2
# 可选链操作符
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Optional_chaining
# in 操作符
如果指定的属性在指定的对象或其原型链中,则**in 运算符**返回true。
in vs hasOwnProperty
in判断的是对象的所有属性,包括对象实例及其原型的属性; 而hasOwnProperty则是判断对象实例的是否具有某个属性。
# 运算符优先级 (opens new window)
← 数据类型判断 同步和异步与宏任务和微任务 →