0Pricing
JavaScript Academy · Lesson

Optional Calls and Index Access

Use optional chaining with calls and brackets.

Beyond Property Reads

Optional chaining also has forms for calling functions and accessing array or dynamic-key elements safely.

const obj = { greet: () => "hi" };
console.log(obj.greet?.()); // "hi"

Optional Method Calls

The ?.() form calls a function only if it exists. If the method is nullish, it returns undefined instead of throwing.

const obj = {};
console.log(obj.greet?.()); // undefined, no error

All lessons in this course

  1. Optional Chaining with the ?. Operator
  2. Nullish Coalescing with ??
  3. Combining ?. and ??
  4. Optional Calls and Index Access
← Back to JavaScript Academy