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

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

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

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

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

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


当前回答

现在,它在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

其他回答

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

我试图检查输入是否为空,然后相应地使用该值。这是我的密码。

let valueToBeConsidered = !inputValue ? "trueCondition" : "falseCondition",

因此,如果inputValue为空,则valueToBeConsidered=false条件;如果inputValue具有值,则valuetoBeConsided=true条件

是的,它很快就要来了。请参阅此处的提案和此处的实施状态。

它看起来像这样:

x ?? y

实例

const response = {
  settings: {
    nullValue: null,
    height: 400,
    animationDuration: 0,
    headerText: '',
    showSplashScreen: false
  }
};

const undefinedValue = response.settings?.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings?.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings?.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings?.showSplashScreen ?? true; // result: false

需要支持旧浏览器并具有对象层次结构

body.head.eyes[0]  //body, head, eyes  may be null 

可以使用此,

(((body||{}) .head||{}) .eyes||[])[0] ||'left eye'

链接多个值/多个值

启用“短路”:如果第一个值之一有效,则不进行任何进一步评估这意味着顺序很重要,最左边的值优先

const value = first ?? second ?? third ?? "default";