Double double questionmarks in TypeScript

Viewed 7214

I have found such kind of code:

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

What ?? ?? what does mean the syntax?

1 Answers

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

Related