Double question mark in Typescript & Javascript | Nullish Coalescing (??) Operator
If you see double question mark (??) in typescript or javascript code and wonder what the hack is this? Well let me tell you that this is a is a logical operator being introduced in ECMAScript 2020 and new typescript version 3.7
Usage of ?? Sign in JavaScript and TypeScript
It returns second parameter when first parameter happens to be undefined or null.
const value = firstParam ?? secondParam;
What's the difference between ?? and || in JavaScript / TypeScript
A common approach to achieve similar thing we use:
const value = firstParam || secondParam;
But problem is logical operator evaluates false value as well, which is not null or undefined
const foo = false const baz = foo || 'default value'; // returns 'default value' const value = foo ?? 'default value'// returns false;
Null coalescing operator in other languages
- C# - ??
- Kotlin - ?:
- PHP (from 7.0) - ??
- Python - or
- Swift - ??
- etc
Read More
- Browser compatibility on MDN
- Tyepscript 3.7 Features
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login .