ควิซ JavaScript: Closures

This JavaScript closures quiz is built from interview questions. You will trace counters and function factories, solve the classic loop and setTimeout puzzle and see how closures keep data private.

ยาก 10 ข้อ ~8 นาที ผ่านที่ 80% Node.js 22

เลือกโหมด

กำลังเล่นแบบผู้เยี่ยมชม: เข้าสู่ระบบเพื่อเก็บ XP และ streak

ตัวอย่างคำถาม

ข้อ 1 จาก 10

What does this print?

function makeCounter() {
  let count = 0;
  return () => ++count;
}
const next = makeCounter();
next();
next();
console.log(next());
ดูเฉลย

D. 3 The returned function keeps a link to count even after makeCounter has finished. Each call increases the same count, so the third call returns 3.

ควิซนี้มีเนื้อหาอะไรบ้าง

  • A function remembers the variables of the scope where it was created
  • Each call to a factory function makes a new, separate closure
  • The var in a loop with setTimeout puzzle and its fixes
  • Closures capture variables, not values
  • Private state, currying and once()

คำถามที่พบบ่อย

ต้องมีบัญชีไหม?

ไม่ต้อง ใครก็เล่นได้ เข้าสู่ระบบเพื่อเก็บ XP, streak, เหรียญตรา และประวัติ

ทำซ้ำได้ไหม?

ได้ ระบบเก็บคะแนนที่ดีที่สุดไว้ และสลับคำถามกับตัวเลือกใหม่ทุกครั้ง