I have found such kind of code:
const dealType = currentDealType ?? originalDealType ?? '';
What ?? ??
what does mean the syntax?
The double question marks (??) are also called nullish coalescing operators and they allow to use of a default value set on the right side of the operator in case the initial value from the left side of the operator is null or undefined .
The JavaScript double question mark (??) operator is called the nullish coalescing operator and it provides a default value when a variable or an expression evaluates to null or undefined.
The question mark ? in typescript is used in two ways: To mention that a particular variable is optional. To pre-check if a member variable is present for an object.
The double question mark operator is called the nullish coalescing operator, and it's a new feature of JavaScript ES2020 that allows you provide a default value to use when a variable expression evaluates to null or undefined .
The JavaScript double question mark is also known as the nullish coalescing operator. It’s an operator that simply returns the right-side expression when the left side expression is either null or undefined.
What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?:) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined. Another way to think about using a question mark with a colon (?:) is to define two types to a property where one of them is undefined.
As question mark marks the variable as optional, but any optional parameter should follow with required parameter. Function here requires any parameter to test if null or undefined. One of the most important point which has been resolved with TypeScript 3.7 version is continuously checking of variables or expressions for being null or undefined
One of the most important point which has been resolved with TypeScript 3.7 version is continuously checking of variables or expressions for being null or undefined Let us consider few examples below to know much better on the implementation and the use of TypeScript question mark.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With