Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
What is *actually* happening within this stale closure?

I feel as though my knowledge with closures is almost there, but I'm struggling to grasp exactly why this first code snippet logs 0 (is a stale closure) however, the second code snippet works and logs the updated value ..

Is this some kind of reference issue?

In the first example, are we not tracking the reference of count and only assigning its initial value to message?

By moving the message variable inside of the log function are we somehow telling JS to track the reference of the count value?

function createIncrement() {
  let count = 0;

  function increment() {
    count++;
  }

  let message = count;
  function log() {
    console.log(message);
  }

  return [increment, log];
}

const [increment, log] = createIncrement();

increment();
log();

// this will log 0

function createIncrement() {
  let count = 0;

  function increment() {
    count++;
  }

  function log() {
    let message = count;
    console.log(message);
  }

  return [increment, log];
}

const [increment, log] = createIncrement();

increment();
log();

// this will log 1

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In the first example, are we not tracking the reference of count and only assigning its initial value to message?
By moving the message variable inside of the log function are we somehow telling JS to track the reference of the count value?

This is the thing you're struggling with. Let's try to decompose what happens there.

At its core, it's just about this:

let count = 0;
let message = count;
console.log(message);
count++; // it's exactly the same as count = count + 1;
console.log(message); // still prints 0

This has nothing to do with closures, and everything to do about numbers being immutable values. When we increment count we are assigning to the variable a new instance of a number with the value 1. message is still pointing to the old instance (value, in this case it's the same), so the log is going to print the same old value.

If I understand your misunderstanding correctly (!) you're picturing that count is a container in which you can insert a numeric value, which is slightly wrong.

It is a pointer to a numeric and immutable value, that sits in memory somewhere. When you do message = count you ensure message and count are pointing to the same memory address, that contains a 0.

When you write count++, you are allocating a new number in memory, somewhere, for the number 1, and count now points to the new object. message is unaffected by this operation. This is the crucial bit that I think is not crystal clear.

In your second example, message is reassigned every time you log, so it will always hold a reference to the correct value.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!