As we know, ES2020 supports optionalChaining. For example, we can write codes like: let x = {a:{b:'c'}}; console.log(x?.a?.b); and we will get 'c'.
But in some cases, we may want to use the expression like x[a][b] rather than x.a.b. Can we use something like x?[a]?[b] to evaluate the validation of each chained object?
The syntax is the same for brackets notation:
x?.[a]?.[b]
Example:
const x = {a:{b:'c'}};
const a = 'a';
const b = 'b';
const result = x?.[a]?.[b];
console.log(result);