Skip to content

OutputNot attempted

Method chaining, then detaching

const counter = {
count: 0,
inc() {
this.count++;
return this;
},
};
console.log(counter.inc().inc().count);
const inc = counter.inc;
try {
inc();
} catch (e) {
console.log(e.name);
}

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Returning this enables chaining → 2. Detached inc() has this = undefined, so this.count throws TypeError (reading a property of undefined).