What is the difference between "let" and "var"?

In JavaScript, both let and var are used to declare variables, but they differ significantly in how they manage scope, hoisting, and redeclaration. These differences affect how and where you can use them within your code. Understanding these distinctions is crucial for managing variables properly in JavaScript applications.

1. Scope

The key difference between let and var is their scoping rules.

  • var: Declares a variable that has function scope or globally scoped if not declared inside a function. This means that a variable declared with var inside a function is only accessible within that function, whereas if it is declared outside any function, it is available globally.
if (true) { var varVariable = "I am var"; } console.log(varVariable); // Outputs: "I am var" (var ignores block scope)
  • let: Introduces block scoping to JavaScript. A variable declared with let can only be accessed within the block {} in which it was declared, which could be a function, if block, loop, etc.
if (true) { let letVariable = "I am let"; } console.log(letVariable); // Uncaught ReferenceError: letVariable is not defined

2. Hoisting

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase, before the code has been executed.

  • var: Variables declared with var are hoisted to the top of their functional/global scope and initialized with a value of undefined.
console.log(foo); // Outputs: undefined var foo = "bar";
  • let: Variables declared with let are hoisted to the top of their block scope, but they are not initialized. Accessing the variable before the declaration results in a ReferenceError. The time between the beginning of the block and the initialization is known as the "temporal dead zone."
console.log(bar); // Uncaught ReferenceError: Cannot access 'bar' before initialization let bar = "foo";

3. Redeclaration

How JavaScript handles redeclaring the same variable within the same scope can lead to potential issues and bugs, especially in larger codebases.

  • var: Allows you to redeclare the same variable multiple times without error, which can lead to bugs.
var baz = "baz"; var baz = "redeclared baz"; // No error
  • let: Does not allow you to redeclare the same variable within the same scope. Attempting to do so results in a SyntaxError.
let qux = "qux"; let qux = "redeclared qux"; // SyntaxError: Identifier 'qux' has already been declared

Conclusion

The introduction of let (and const) in ES6 (ECMAScript 2015) was partly to address the quirks and issues with var. It's generally recommended to use let (or const for variables that should not change) in modern JavaScript due to their clearer scoping, which helps in writing cleaner, more predictable code. In practice, the use of var is now largely seen as outdated and potentially prone to causing bugs due to its less intuitive scoping and hoisting behaviors.

TAGS
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is a Shopify life story interview?
Why am I interested in working for CrowdStrike?
What is the difference between a table and a schema?
What are the 4 software engineering activities?
What are the strategies for improving coding interview performance over time?
Which is the toughest language in coding?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.