Javascript中是否有空合并运算符?

例如,在C#中,我可以这样做:

String someString = null;
var whatIWant = someString ?? "Cookies!";

我可以为Javascript找到的最佳近似是使用条件运算符:

var someString = null;
var whatIWant = someString ? someString : 'Cookies!';

这有点讨厌IMHO。我能做得更好吗?


当前回答

?? vs | | vs&&

其他答案中没有一个能比较这三个答案。由于贾斯汀·约翰逊(Justin Johnson)的评论获得了如此多的选票,而且javascript中的双问号vs&&被标记为这一条的重复,因此在答案中包含&&是有意义的。

首先,受贾斯汀·约翰逊评论的启发:

||返回第一个“truey”值,否则返回最后一个值。&&返回第一个“false”值,否则返回最后一个值。?? 返回第一个非空的、未定义的值,否则返回最后一个值,无论它是什么。

然后,用实时代码演示:

设F1,F2=空,F3=0,F4=“”,F5=parseInt('不是数字(NaN)'),T1=3,T2=8控制台日志(F1||F2||F3||F4||F5||T1||T2)//3(T1)console.log(F1||F2||F3||F4||F5)//NaN(F5)console.log(T1&&T2&&F1&&F2&&F3&&F4&&F5)//未定义(F1)console.log(T1和T2)//8(T2)控制台日志(F1??F2??F3??F4??F5??T1)//0(F3)console.log(F1??F2)//空(F2)

其他回答

ECMAScript 2021启用了两个新功能:

空合并运算符(??),是一种逻辑运算符,当左侧操作数为空或未定义时,返回右侧操作数,否则返回左侧操作数。

设b=未定义??5.console.log(b);//5.

逻辑零赋值(x??=y)运算符,仅在x具有零值(空或未定义)时赋值。

常量车={速度:20};车速??=5.控制台日志(轿厢速度);汽车名称??=“雷诺”;console.log(car.name);

有关逻辑零赋值的更多信息,请参阅此处https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment

有关空合并运算符的更多信息,请参阅此处https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

如果||作为C#??的替代品??在您的情况下还不够好,因为它包含空字符串和零,所以您可以始终编写自己的函数:

 function $N(value, ifnull) {
    if (value === null || value === undefined)
      return ifnull;
    return value;
 }

 var whatIWant = $N(someString, 'Cookies!');

注意,React的create-React-app工具链支持自3.3.0版(发布于5.12.2019)以来的空合并

可选的链接和空合并运算符我们现在支持可选的链接和无效合并运算符!//可选链接一(); // 如果“a”为空/未定义,则为undefinedbc、 //如果“b”为空/未定义,则未定义//空聚结未定义??'其他默认值';//result:'其他默认值'空??'其他默认值';//result:'其他默认值''' ?? '其他默认值';//结果:“”0 ?? 300; // 结果:0假的??真;//结果:false

也就是说,如果你使用create-react-app3.3.0+,你可以开始在你的react应用中使用空联合运算符。

现在,它在Chrome、Edge、Firefox、Safari等主流浏览器的最新版本中得到了完全支持。下面是空运算符和空合并运算符的比较

const response = {
        settings: {
            nullValue: null,
            height: 400,
            animationDuration: 0,
            headerText: '',
            showSplashScreen: false
        }
    };
    /* OR Operator */
    const undefinedValue = response.settings.undefinedValue || 'Default Value'; // 'Default Value'
    const nullValue = response.settings.nullValue || 'Default Value'; // 'Default Value'
    const headerText = response.settings.headerText || 'Hello, world!'; //  'Hello, world!'
    const animationDuration = response.settings.animationDuration || 300; //  300
    const showSplashScreen = response.settings.showSplashScreen || true; //  true
    /* Nullish Coalescing Operator */
    const undefinedValue = response.settings.undefinedValue ?? 'Default Value'; // 'Default Value'
    const nullValue = response.settings.nullValue ?? ''Default Value'; // 'Default Value'
    const headerText = response.settings.headerText ?? 'Hello, world!'; // ''
    const animationDuration = response.settings.animationDuration ?? 300; // 0
    const showSplashScreen = response.settings.showSplashScreen ?? true; //  false

这里有两项:

逻辑或

const foo=“”|“默认字符串”;

console.log(foo);//输出为“默认字符串”

空合并运算符

常量foo=''??'默认字符串';

console.log(foo);//输出为空字符串,即“”

零合并运算符(??)是一种逻辑运算符,当左侧操作数为空或未定义时,它返回右侧操作数,否则返回左侧操作数。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator