Question mark after parameter as in obj.val?.prop [duplicate]

I met in code condition line like this someObject.arrParam?.length. What syntax is that? How does that question mark thing's called? I know an optional operator which used for parameters in functions. Is that a variation of usage of it? Never met before.


This is called Optional Chaining in JavaScript. It allows to drill down on objects without raising null exception.

Eg: Try running the below code snippet, then uncomment the line and run it to understand a working example.

let employeeA ={ name: "Dane", address : { city:"London"}}
let employeeB ={ name: "John"}

console.log(employeeA.address.city)
// console.log(employeeB.address.city) <----  this will raise an error
console.log(employeeB.address?.city) // <--- this wont

This was introduced as new feature in the latest ESNext iterations.

NodeJS Support : https://node.green/#ES2020-features-optional-chaining-operator-----

Current Browser Support : https://caniuse.com/#feat=mdn-javascript_operators_optional_chaining

More Details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining


That is called Optional Chaining (or conditional chaining) which basically will evaluate the whole expression as undefined if arrParam is undefined or null.