typescript

Double double questionmarks in TypeScript


I found this code:

const dealType = currentDealType ?? originalDealType ?? '';

What does the ?? ?? syntax mean?


Solution

  • It's the nullish coalescing operator that has been proposed for ecmascript and has been implemented in Typescript. You can read more here or here

    The gist of it is that

    const dealType = currentDealType ?? originalDealType;
    

    is equivalent to:

    const dealType = currentDealType !== null && currentDealType !== void 0 ? currentDealType : originalDealType;
    

    Or in words: if currentDealType is null or undefined use originalDealType otherwise use currentDealType