🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
For example, if obj.first is a Falsy value that's not null or undefined, such as 0, it would still short-circuit and make nestedProp become 0, which may not be desirable. With the optional chaining operator (?.), however, you don't have to explicitly test and short-circuit based on the state of obj.first before trying to access obj.first.second: ... By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
🌐
Stack Overflow
stackoverflow.com › questions › 72271204 › using-questionmark-to-check-if-variable-is-null-or-undefined-bad-practice
typescript - Using questionmark (?) to check if variable is null or undefined - bad practice? - Stack Overflow
I heard from a colleague that it is bad practice to use a question mark to check if a variable is null or undefined and in the same turn call a property / method on this. For example. let text ...
🌐
freeCodeCamp
freecodecamp.org › news › how-the-question-mark-works-in-javascript
How the Question Mark (?) Operator Works in JavaScript
February 3, 2021 - If we use (||), it will be considered as undefined or NULL and return some default value that we have fixed. Instead of the logical OR (||) operator, you can use double question marks (??), or Nullish Coalescing.
🌐
CoreUI
coreui.io › blog › what-is-double-question-mark-in-javascript
What is Double Question Mark in JavaScript? · CoreUI
March 12, 2024 - You want to use the user’s name if provided; otherwise, you’d default to a friendly “Stranger.” The double question mark operator is your go-to tool for this task. It checks if a value is null or undefined and, based on that check, either ...
🌐
Ponyfoo
ponyfoo.com › articles › null-propagation-operator
Null Propagation Operator in JavaScript — Pony Foo
Or we use a library like lodash ... get(person, ['profile', 'name', 'firstName']) The Null Propagation operator is a native solution to the problem, allowing us to handle these cases by sprinkling our code with question marks......
🌐
Flexiple
flexiple.com › javascript › double-question-mark-javascript
The Double Question Mark (??) in JavaScript - Flexiple
The double question mark in JavaScript, known as the nullish coalescing operator, operates by returning the right-hand operand when the left-hand operand is either null or undefined.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-question-mark-operator
JavaScript Question Mark (?) Operator Explained | Built In
The question mark operator ? takes three operands: a condition, a value if that condition is true and a value if that condition is false. It’s used in JavaScript to shorten an if else statement to one line of code.
🌐
Eran Stiller
eranstiller.com › javascript-double-question-marks
What Is the JavaScript Double Question Marks (??) Operator?
September 24, 2023 - I’ll cover this scenario in the section below. You should use the JavaScript double question marks operator when you want to assign a default value in case the initial value is null or undefined.
🌐
sebhastian
sebhastian.com › javascript-double-question-mark
Learning JavaScript double question mark (??) or the Nullish Coalescing Operator | sebhastian
January 23, 2022 - The double question mark is a logical operator that returns the expression on the right-hand of the mark when the expression on the left-hand is null or undefined · This operator is also known as the Nullish Coalescing Operator.
🌐
Scaler
scaler.com › home › topics › what is a nullish coalescing operator or double question mark (??) in javascript?
What is JavaScript double question mark (??) - nullish coalescing operator - Scaler Topics
March 28, 2024 - Nullish coalescing is denoted by double question marks (??). ... In simple words, ?? returns the left argument if it’s not null/undefined. Otherwise, the right one. When a passed parameter is less than the number of parameters specified in ...
🌐
Plain English
plainenglish.io › blog › javascript-operator
The JavaScript ?? (Nullish Coalescing) Operator: How Does it Work?
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.
🌐
Codedamn
codedamn.com › news › javascript
Double question mark in JavaScript (Nullish coalescing operator)
January 5, 2023 - However, there are times when the logical OR operator doesn’t quite cut it, and that’s where the nullish coalescing operator (??) comes in. It is a newer addition to JavaScript (introduced in the ES2020 specification) that explicitly checks for null or undefined values rather than any falsy value.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - The double question mark is a logical operator that returns the expression on the right-hand side of the mark when the expression on the left-hand side is null or undefined · This operator is also known as the nullish coalescing operator.
🌐
JavaScript in Plain English
javascript.plainenglish.io › 3-uses-of-in-javascript-why-pro-developers-love-using-javascript-operator-565bc8b235a4
3 Uses of '?' in JavaScript
August 2, 2024 - 'LOL'; true || undefined ?? 'Hello World'; ... The third operator is my favorite on this list. The JavaScript question mark dot ( ?. ) operator, is known as the Optional Chaining operator.
Top answer
1 of 15
432

Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:

At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null or undefined. The star of the show in optional chaining is the new ?. operator for optional property accesses.

Refer to the TypeScript 3.7 release notes for more details.


Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).

As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript and Swift), there are a couple of other examples:

  • CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

  • C# calls this a null-conditional operator.

a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • Kotlin refers to it as the safe call operator.

There are probably lots of other examples, too.

2 of 15
157

It is now possible, see answer of user "Donut".

Old answer: Standard JavaScript behaviour regarding boolean operators has something that may help. The boolean methods do not return true or false when comparing objects, but in case of OR the first value that is equal to true.

Not as nice as a single ?, but it works:

var thing = foo && foo.bar || null;

You can use as many && as you like:

var thing = foo && foo.bar && foo.bar.check && foo.bar.check.x || null;

Default values are also possible:

var name = person && person.name || "Unknown user";
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript nullish coalescing operator
JavaScript Nullish Coalescing Operator
October 6, 2023 - Summary: in this tutorial, you’ll ... or undefined. ES2020 introduced the nullish coalescing operator denoted by the double question marks (??)....