【发布时间】:2022-01-01 06:15:24
【问题描述】:
根据definition nullish assignment ,仅当变量为 null 或未定义时才分配。但是,也可以使用 OR 运算符完成相同的操作,如下例所示。我们应该在哪里使用空赋值运算符而不是逻辑 OR 运算符?
let a = null;
let b = undefined;
a ||= 10;
console.log(a); // output: 50
b ||= 'string is empty.';
console.log(b); // output: "string is empty."
let a = null;
let b = undefined;
a ??= 10;
console.log(a); // output: 50
b ??= 'string is empty.';
console.log(b); // output: "string is empty."
【问题讨论】:
标签: javascript