JS Quiz: Optional chaining with side effects

What is printed by the final console.log?

const x = {
  value: 0,
  inc() {
    this.value++;
    return this.value;
  }
};

const a = x.inc?.();
const b = (x.missing?.()) ?? 42;
console.log(a, b, x.value);
  • 1 42 1
  • 1 undefined 1
  • undefined 42 0
  • TypeError is thrown
0 voters

BayMax

Choosing “1 42 1” because x.inc..() still calls the existing method and increments value, while x.missing..() short-circuits to undefined so ..

WaffleFries