Optional chaining should be written as below

a?.[0]?.b?.[0].prop

Answer from Hardik Shah on Stack Overflow
🌐
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
Check out the docs for optional chaining: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ... "I see that the console logs undefined, which is probably a unintended behaviour" very much intended and as per spec. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags.
🌐
Flexiple
flexiple.com › javascript › double-question-mark-javascript
The Double Question Mark (??) in JavaScript - Flexiple
In summary, use ?? when you need to handle null or undefined without interfering with other falsy values. Use || when any falsy value should trigger the use of a default. Combining the double question mark (??) with other operators in JavaScript can enhance nullish value handling in expressions...
Discussions

javascript - null checking of array using question mark break - Stack Overflow
, I can use something like lodash's get or check using the old method but how to use the question mark syntax? ... Alternatively, you could use || operator to get to the desired value. ... The December 2024 Community Asks Sprint has been moved to March 2025 (and... Stack Overflow Jobs is expanding to more countries · 4 JavaScript... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to check for null inline in javascript? - Stack Overflow
I have a function which parses the address components of the Google Maps API JSON and then returns the city / locality / route name. The getAddressComponent() returns a null if it cannot find the... More on stackoverflow.com
🌐 stackoverflow.com
One Question Poll: If x === null, what do you expect x?.y to be?
For the curious, this poll comes out of a TC39 discussion on the expected behavior for the new optional-chaining operator that will allow null-safe property access: https://github.com/tc39/proposal-optional-chaining/issues/65 More on reddit.com
🌐 r/javascript
63
39
June 27, 2018
What does the question mark in the context of this expression mean: array?.length
This is known as optional chaining. If array exists it will access the length property otherwise it will evaluate to undefined. This is used to prevent run time errors, 'Cannot read property length of undefined' More on reddit.com
🌐 r/learnjavascript
16
57
October 10, 2021
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
3 weeks ago - If you expect that someInterface itself may be null or undefined, you have to use ?. at this position as well: someInterface?.customMethod?.(). eval?.() is the shortest way to enter indirect eval mode. You can also use the optional chaining operator with bracket notation, which allows passing an expression as the property name:
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
For example, here we show user if its value isn’t null/undefined, otherwise Anonymous: let user; alert(user ?? "Anonymous"); // Anonymous (user is undefined) Here’s the example with user assigned to a name:
🌐
Enji
enji.systems › 2025 › 03 › 13 › question-marks-typescript.html
Question Marks in JavaScript and TypeScript | Enji’s Blog
March 13, 2025 - 'right'); // left console.log(null ?? 'right'); // right console.log(undefined ?? 'right'); // right console.log('' ?? 'right'); // '' The ?? operator is stricter than the logical OR operator ||, which doesn’t only check for null / undefined, but also for “falsy” values (such as empty strings, or 0).
🌐
Ponyfoo
ponyfoo.com › articles › null-propagation-operator
Null Propagation Operator in JavaScript — Pony Foo
July 31, 2017 - 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......
Find elsewhere
🌐
Plain English
plainenglish.io › blog › javascript-operator
The JavaScript ?? (Nullish Coalescing) Operator: How Does it Work?
January 19, 2022 - Using the nullish coalescing operator, or double question mark, is fairly straightforward. When you see the JavaScript ?? operator, follow this logic: IF the first value is null/undefined THEN use the default value. ... JavaScript ?.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript - MDN Web Docs
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.
🌐
freeCodeCamp
freecodecamp.org › news › how-the-question-mark-works-in-javascript
How the Question Mark (?) Operator Works in JavaScript
February 3, 2021 - But if we replace '||' with '??', we will get 0 and an empty string, which makes it so cool. ... Nullish Coalescing works exactly like the logical OR operator, except you will get the right side value when the left side value is undefined or null. In other words, ?? only allows undefined and null values, not empty strings ('') or 0s. Now hopefully you understand how the ? operator works in JavaScript.
🌐
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 - When the left-hand side operand is any false value, not just null or undefined, the right-hand side operand is returned in Nullish coalescing case. Operator precedence for the nullish coalescing operator is the fifth-lowest.
🌐
DEV Community
dev.to › danywalls › simplify-your-typescript-code-with-optional-chaining-and-nullish-coalescing-37on
Simplify Your Typescript Code with Optional Chaining and Nullish Coalescing - DEV Community
May 23, 2023 - If it does, we use its value; ... coalescing: ... The double question mark (??) checks if the left-hand side value is nullish (null or undefined) and provides the right-hand side value as the default if needed....
🌐
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 ...
🌐
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.
🌐
Becomebetterprogrammer
becomebetterprogrammer.com › javascript-how-the-question-mark-works
JavaScript | How does the Question Mark(?) Work? (examples) - Become A Better Programmer
August 10, 2022 - Another scenario where the question mark (?) is used in JavaScript is in the null coalescing operator. The two question mark symbol (??) forms the null coalescing operator. The operator takes two operands on either side.
Top answer
1 of 8
143

2020 Answer, It Exists!!!

You can now directly use ?. inline to test for existence. It is called the Optional Chaining Operator, supported by all modern browsers.

If a property exists, it proceeds to the next check, or returns the value. Any failure will immediately short-circuit and return undefined.

const example = {a: ["first", {b:3}, false]}

example?.a  // ["first", {b:3}, false]
example?.b  // undefined

example?.a?.[0]     // "first"
example?.a?.[1]?.a  // undefined
example?.a?.[1]?.b  // 3

domElement?.parentElement?.children?.[3]?.nextElementSibling

To ensure a default defined value, you can use ??. If you require the first truthy value, you can use ||.

example?.c ?? "c"  // "c"
example?.c || "c"  // "c"

example?.a?.[2] ?? 2  // false
example?.a?.[2] || 2  // 2

If you do not check a case, the left-side property must exist. If not, it will throw an exception.

example?.First         // undefined
example?.First.Second  // Uncaught TypeError: Cannot read property 'Second' of undefined

?. Browser Support - 94%, Oct '22

?? Browser Support - 94%

Node Support - v14+

2 of 8
31

Update 2020

This long-wished feature is now available in JavaScript!

I'll redirect to Gibolt's answer, which covers it well.

Original 2018 answer

  • There is no "null-safe navigation operator" in Javascript (EcmaScript 5 or 6), like ?. in C#, Angular templates, etc. (also sometimes called Elvis operator, when written ?:) , at least yet, unfortunately.

  • You can test for null and return some dependent expression in a single line with the ternary operator ?:, as already given in other answers :

(use === null to check only for nulls values, and == null to check for null and undefined)

    console.log(myVar == null ? myVar.myProp : 'fallBackValue');
  • in some cases, like yours, when your variable is supposed to hold an object, you can simply use the fact that any object is truthy whereas null and undefined are falsy values :

      if (myVar) 
          console.log(myVar.myProp)
      else
          console.log('fallbackValue')
    

    You can test for falsy values by coalescing to boolean with !! and make this inline :

      console.log(!!myVar ? myVar.myProp : 'fallbackValue');
    

    Be very careful though with this "falsy test", for if your variable is 0, '', or NaN, then it is falsy as well, even though it is not null/undefined.

🌐
Codedamn
codedamn.com › news › javascript
What is the Question Mark in JavaScript? Explained with Examples
February 15, 2023 - If it is, we return the string ‘Cheers 🍻’. If not, we return the string ‘Sorry, no drinks for you 🚫’. The question mark symbol is also used in the nullish coalescing operator.
🌐
Eran Stiller
eranstiller.com › javascript-double-question-marks
What Is the JavaScript Double Question Marks (??) Operator?
September 24, 2023 - It’s a more efficient, readable way to handle default value assignments in JavaScript. You can use the JavaScript double question marks operator to handle missing function arguments within the function body.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript - MDN Web Docs
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.