What is the difference between "let" and "var"?
Published
9 Dec 2022
Video
Overview
There are three main differences when declaring a variable with let vs var.
| let | var | |
|---|---|---|
| Scope | block scoped | function scoped |
| Can Redeclare | No | Yes |
| Hoisting | No | Yes |
Scope
Global Scope
Variables declared Globally (outside any function or block) have Global Scope.
Global variables can be accessed from anywhere in a JavaScript program. Variables declared with var and let behave quite similarly when declared in this context.
var a = "var defuined";let b = "let defuined";
console.log(a + " " + b); // can be accessed from here
function exampleFunction() { console.log(a + " " + b); // can be accessed from here { console.log(a + " " + b); // can be accessed from here }}
console.log(a + " " + b); // can be accessed from hereCode Example
Function Scope
Similarly, variables defined with both var and let inside a Function are scoped to that function and are not accessible outside the Function Scope but can be accessed from any sub-scope.
function exampleFunction() { var a = "var defuined"; let b = "let defuined"; console.log(a + " " + b); // can be accessed from here { console.log(a + " " + b); // can be accessed from here }}
console.log(a + " " + b); // can't be accessed from hereCode Example
Block Scope
The difference comes when variables are defined within a Block. Variables declared inside a { } block with let cannot be accessed outside the block scope, whereas variables defined with var can.
{ var a = "var defuined"; let b = "let defuined"; console.log(a); // can be accessed from here console.log(b); // can be accessed from here} console.log(a); // can be accessed from hereconsole.log(b); // can't be accessed from hereCose Example
Can Redeclare
A variable declared with var can be redeclared over and over again. A variable declared with let cannot be redeclared within the same scope and will throw an error.
var a = 1; // 1let b = 1; // 1
var a = 2; // 2let b = 2; // SyntaxError: Identifier 'b' has already been declared.Code Example
Redeclaring a variable with var in a different scope or block also changes the value of the outer variable. This behaviour can produce some of the most challenging errors to debug.
Redeclaring a variable with let in a different scope or block treats that variable as a different new variable. And the value of the variable outside does not change.
var a = 1;let b = 1;console.log('a =', a); // 1console.log('b =', b); // 1
{ var a = 2; let b = 2; console.log('a =', a); // 2 console.log('b =', b); // 2}
console.log('a =', a); // 2console.log('b =', b); // 1Code Example
'a =' 1'b =' 1
'a =' 2'b =' 2
'a =' 2'b =' 1Output
š”
Note: Reusing the same variable name within inner scope can make code harder to read and debug. Its worth avoiding this if possible
Hoisting
Unlike languages like C, variables and functions in JavaScript are not declared at the point of use.
Instead, no matter where they are declared, variables defined with the var keyword are moved to the top of their scope and assigned a default value of undefined until they're reached in code.
console.log(a); // undefinedvar a = "Hoisted";console.log(a); // HoistedExample
console.log(a); // undefinedvar a = "Hoisted";console.log(a); // HoistedOutput
Variables defined with let do not allow hoisting and will throw an error if accessed before the declaration.
console.log(a);let a; // Uncaught ReferenceError: a is not definedIf you want to learn more about hoisting, take a look at my variable-and-function-hoisting article.
Conclusion
The let keyword was introduced in the later version of JavaScript known as ES6(ES2015). And due to its straightforward approach to scope and ability to produce more readable and maintainable code, let is the preferred way to declare variables.
Similar articles

Pathfinding Algorithms Explained
Get from the entrance to the exit without a patrolling guard spotting you. It sounds like a game mechanic, but it's a clean, hands-on way to see how AI search actually works: seven algorithms from a blind random walk up to A*, with a live demo that learns from every failed attempt.
26 July 2026

Tokens, Vectors, and Attention: How an LLM Actually Reads Your Prompt
An LLM never sees your words. It sees numbers: token IDs, vectors, and probabilities. Here's what actually happens between you hitting enter and the model's reply appearing, one step at a time.
26 July 2026

SOLID Principles Matter More With AI Coding Agents, Not Less
SOLID has been sitting in OOP textbooks for twenty years, half-forgotten by JavaScript developers who never wrote much Java. Hand a codebase to an AI agent and those five old rules turn into the difference between a codebase that keeps shipping and one that quietly rots.
25 July 2026

AI Liability: Who Is Responsible When AI Gets It Wrong?
I'm a software engineer, not a lawyer. But I've been watching the AI liability conversation closely, and the gap between how AI systems actually work and how our legal system assigns blame is becoming a real operational problem for anyone deploying AI.
17 July 2026
