← Back to archive

Stdle #156 · scope · 2026-11-03

What does this JavaScript snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

function once(fn) {
let called = false, result;
return (...a) => called ? result : (called = true, result = fn(...a));
}
let n = 0;
const init = once(() => ++n);
console.log(init(), init(), n);
Scope
4 attempts left

Answer & explanation