What is the difference between "let" and "var" in JavaScript?
Understanding the Scope and Behavior Differences Between let
and var
In JavaScript, both var
and let
are used to declare variables, but they differ in how they handle scoping, redeclaration, and variable hoisting. The introduction of let
in ES6 (ECMAScript 2015) was part of a broader effort to address longstanding quirks associated with var
and to encourage cleaner, more predictable code.
Key Differences
-
Scope
var
: Variables declared withvar
are function-scoped. If declared outside any function, they become globally scoped. This means that if you declare avar
variable inside a block (e.g., inside anif
statement), it’s still accessible outside that block.let
: Variables declared withlet
are block-scoped. They only exist within the nearest set of braces, such as inside a function,if
statement, orfor
loop. This leads to more predictable and self-contained code blocks.
-
Redeclaration
var
: You can redeclare the same variable multiple times without an error, which can lead to accidental overwriting of variables.let
: You cannot redeclare a variable usinglet
within the same scope. Attempting to do so results in aSyntaxError
, helping prevent unintentional variable shadowing or overwriting.
-
Hoisting
var
:var
declarations are hoisted to the top of their function scope, but not their initializations. This means you can reference avar
variable before its line of declaration without throwing aReferenceError
, although its value will beundefined
until the declaration line is reached.let
:let
declarations are also hoisted to the top of their block scope, but remain in a “temporal dead zone” (TDZ) until their actual declaration line is executed. Accessing alet
variable before it’s declared results in aReferenceError
, ensuring clearer and more predictable behavior.
Example
var
Example:
console.log(x); // undefined (due to hoisting) var x = 10; if (true) { var y = 20; } console.log(y); // 20, accessible outside the if-block
let
Example:
console.log(a); // ReferenceError: Cannot access 'a' before initialization let a = 10; if (true) { let b = 20; } console.log(b); // ReferenceError: b is not defined (b is block-scoped)
Modern Best Practices
Since let
provides block scope and more predictable behavior, it’s generally preferred over var
in modern JavaScript code. const
—another ES6 addition—goes one step further by creating block-scoped variables that cannot be reassigned, promoting even safer and more stable code.
Strengthen Your JavaScript Foundations
To confidently navigate JavaScript features like let
and var
—and to fully understand fundamental concepts such as scoping, hoisting, and variable lifecycles—consider building a strong foundation in the language:
- Grokking JavaScript Fundamentals: Perfect for beginners or those looking to refresh their knowledge, this course ensures you understand modern JavaScript best practices, language patterns, and core principles. By mastering these essentials, you’ll write more robust, maintainable, and future-proof code.
In Summary
- Scope:
var
is function-scoped, whilelet
is block-scoped. - Redeclaration:
var
allows redeclaration;let
does not. - Hoisting: Both are hoisted, but
let
enforces a temporal dead zone, preventing access before declaration.
Embrace let
(and const
) for more predictable, cleaner, and more maintainable code as you refine your JavaScript expertise.