|
- 空值合并运算符(??) - JavaScript | MDN
空值合并运算符 (??)是一个逻辑运算符,当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数。 空值合并运算符可以视为 逻辑或运算符(||) 的特例。 后者在左侧操作数为 任何 假值 时返回右侧操作数,而不仅仅是 null 或 undefined。 换句话说,如果你使用 || 为另一个变量 foo 提供某些默认值,而你将某些假值视为可用值(例如 '' 或 0),则可能会遇到意外的行为。 更多示例参见 下方。 空值合并运算符的 运算符优先级 是第五低的,直接低于 || 且直接高于 条件(三元)运算符。 将 ?? 直接与逻辑与( )和逻辑或(||)运算符组合使用是不可取的。 这种情况下会抛出 语法错误。 相反,请提供括号以明确表示优先级:
- ?? and ??= operators - null-coalescing operators - C# reference
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null
- Null coalescing operator - Wikipedia
It is most commonly written as x ?? y, but varies across programming languages While its behavior differs between implementations, the null coalescing operator generally returns the result of its left-most operand if it exists and is not null, and otherwise returns the right-most operand
- Nullish coalescing operator - The Modern JavaScript Tutorial
The nullish coalescing operator is written as two question marks ?? As it treats null and undefined similarly, we’ll use a special term here, in this article
- Nullish coalescing operator - JavaScript中文版 - API参考文档
JavaScript Demo: Expressions - Nullish coalescing operator x 1 const foo = null ?? 'default string';
- 什么是JavaScript中的 “空凝聚 “运算符 - 极客教程
空值凝聚运算符是一个有用的工具,可以在你的代码中提供默认值和处理空值或未定义值。 以下是JavaScript中空值凝聚运算符 (??)的语法- 这里, operand1 和 operand2 是两个操作数。 如果操作数 (??)存在并且不是空或未定义,则返回 operand1。 否则,它将返回 operand2。 让我们看一个例子,我们创建一个常量并将其分配给字符串 “Hello “用null凝聚运算符将其分开,然后提供一个字符串 “Hello World”。 左边的值不是空的,所以它将被分配给常量。
- ES11 中 Nullish Coalescing Operator 的应用场景和使用方法
Nullish Coalescing Operator,中文翻译为“空值合并运算符”。 它由两个问号(??)组成,用于判断一个变量是否为 null 或 undefined,并给出一个默认值,如果变量不为 null 或 undefined,则返回该变量;否则返回默认值。
- Nullish coalescing operator (??) - JavaScript - MDN
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand
|
|
|