Search⌘ K
AI Features

Discussion: The Chain Master

Explore how the optional chaining operator (?.) works in JavaScript to safely access nested properties without causing errors. Understand its behavior in the context of a puzzle where the operator prevents incrementing a variable due to null values, helping you write cleaner, more reliable code.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node-es2024)
const titles = null;
let x = 0;
titles?.[++x].toUpperCase();
console.log(x);

Understanding the output

In this puzzle, we attempt to increment x by 1 through optional chaining, but since titles is ...