A deeper look at block scope and why modern JavaScript prefers let and const over var.
While introduced earlier, it's crucial to solidify the understanding of `let` and `const` as they are foundational to modern JavaScript. The primary advantage they offer over `var` is *block scope*. A block is any section of code enclosed in curly braces `{}`, such as in an `if` statement, a `for` loop, or just a standalone block. Variables declared with `let` or `const` only exist within that block, preventing accidental modifications from outside and reducing the chance of naming conflicts. This is in stark contrast to `var`, which is function-scoped. Another key concept is the 'temporal dead zone' (TDZ). While `var` declarations are hoisted (but not initialized), `let` and `const` declarations are also technically hoisted but they are not initialized. Accessing them before the declaration results in a `ReferenceError`. This helps catch bugs early by preventing you from using a variable before its value is set. The rule of thumb in modern development is to stop using `var` entirely. Default to `const` for all variable declarations. This makes your code more predictable, as it signals that the variable's reference will not be reassigned. Only switch to `let` if you explicitly know that the variable's value needs to change later in its lifecycle.