Skip to content

Q2: improve explanation for question 2 (var vs let hoisting)#845

Open
aalex198 wants to merge 1 commit into
lydiahallie:masterfrom
aalex198:docs/q2-memory-allocation
Open

Q2: improve explanation for question 2 (var vs let hoisting)#845
aalex198 wants to merge 1 commit into
lydiahallie:masterfrom
aalex198:docs/q2-memory-allocation

Conversation

@aalex198

Copy link
Copy Markdown

Hi Lydia! 👋

First of all, thank you so much for this amazing repository! I really appreciate the effort you've put into maintaining it and all the translations. ❤️

Summary

This PR improves the technical explanation for Question 2 (var vs let in a for loop with setTimeout) across all 22 language versions of the README.

Problem

The current explanation states that var makes the variable global and let is block-scoped, but it doesn't explain why var shares a single value across all iterations while let creates a new value per iteration. Understanding the memory allocation difference is key to grasping this classic JavaScript interview question.

Changes

Added one sentence to each paragraph of the answer (existing text was preserved):

  • Paragraph 1 (var): Clarifies that due to hoisting, var allocates a single memory slot for the variable, so each iteration overwrites the same variable rather than creating a new one.
  • Paragraph 2 (let): Clarifies that let creates a new binding (a new memory slot) for each iteration, which is why each setTimeout callback captures a different value.

Why This Matters

The var vs let behavior in loops is one of the most common JavaScript interview questions. The previous explanation correctly described the outcome but didn't explain the underlying mechanism (memory allocation during hoisting). This change makes the explanation more complete and technically accurate for learners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant