Scoping rules

The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar); // Foo Bar

  {
    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz
  }

  console.log(moo); // Mooo
  console.log(baz); // ReferenceError
}

run();

The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.

Take a look at this example from another Stack Overflow question:

var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
  // and store them in funcs
  funcs[i] = function() {
    // each should log its value.
    console.log("My value: " + i);
  };
}
for (var j = 0; j < 3; j++) {
  // and now let's run each one to see
  funcs[j]();
}

My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable.

People had to create immediately invoked functions to capture correct values from the loops but that was also hairy.

Hoisting

Variables declared with var keyword are hoisted and initialized which means they are accessible in their enclosing scope even before they are declared, however their value is undefined before the declaration statement is reached:

function checkHoisting() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

let variables are hoisted but not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. The variable is said to be in the temporal dead zone from the start of the block until the declaration statement is processed.

function checkHoisting() {
  console.log(foo); // ReferenceError
  let foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

Creating global object property

At the top level, let, unlike var, does not create a property on the global object:

var foo = "Foo"; // globally scoped
let bar = "Bar"; // globally scoped but not part of the global object

console.log(window.foo); // Foo
console.log(window.bar); // undefined

Redeclaration

In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.

'use strict';
var foo = "foo1";
var foo = "foo2"; // No problem, 'foo1' is replaced with 'foo2'.

let bar = "bar1"; 
let bar = "bar2"; // SyntaxError: Identifier 'bar' has already been declared

Top answer
1 of 16
8394

Scoping rules

The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar); // Foo Bar

  {
    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz
  }

  console.log(moo); // Mooo
  console.log(baz); // ReferenceError
}

run();

The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.

Take a look at this example from another Stack Overflow question:

var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
  // and store them in funcs
  funcs[i] = function() {
    // each should log its value.
    console.log("My value: " + i);
  };
}
for (var j = 0; j < 3; j++) {
  // and now let's run each one to see
  funcs[j]();
}

My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable.

People had to create immediately invoked functions to capture correct values from the loops but that was also hairy.

Hoisting

Variables declared with var keyword are hoisted and initialized which means they are accessible in their enclosing scope even before they are declared, however their value is undefined before the declaration statement is reached:

function checkHoisting() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

let variables are hoisted but not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. The variable is said to be in the temporal dead zone from the start of the block until the declaration statement is processed.

function checkHoisting() {
  console.log(foo); // ReferenceError
  let foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

Creating global object property

At the top level, let, unlike var, does not create a property on the global object:

var foo = "Foo"; // globally scoped
let bar = "Bar"; // globally scoped but not part of the global object

console.log(window.foo); // Foo
console.log(window.bar); // undefined

Redeclaration

In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.

'use strict';
var foo = "foo1";
var foo = "foo2"; // No problem, 'foo1' is replaced with 'foo2'.

let bar = "bar1"; 
let bar = "bar2"; // SyntaxError: Identifier 'bar' has already been declared

2 of 16
851

let can also be used to avoid problems with closures. It binds fresh value rather than keeping an old reference as shown in examples below.

for(var i=1; i<6; i++) {
  $("#div" + i).click(function () { console.log(i); });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Clicking on each number will log to console:</p> 
<div id="div1">1</div>
<div id="div2">2</div>
<div id="div3">3</div>
<div id="div4">4</div>
<div id="div5">5</div>

Code above demonstrates a classic JavaScript closure problem. Reference to the i variable is being stored in the click handler closure, rather than the actual value of i.

Every single click handler will refer to the same object because there’s only one counter object which holds 6 so you get six on each click.

A general workaround is to wrap this in an anonymous function and pass i as an argument. Such issues can also be avoided now by using let instead var as shown in the code below.

(Tested in Chrome and Firefox 50)

for(let i=1; i<6; i++) {
  $("#div" + i).click(function () { console.log(i); });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Clicking on each number will log to console:</p> 
<div id="div1">1</div>
<div id="div2">2</div>
<div id="div3">3</div>
<div id="div4">4</div>
<div id="div5">5</div>

🌐
Reddit
reddit.com › r/learnjavascript › difference between var, let and const
r/learnjavascript on Reddit: Difference between var, let and const
March 28, 2022 -

Hi everyone!

I'm pretty new to web development and have started to read a little bit about javascript. Right now I'm trying to learn how to declare a function but realized that there is different ways to do this using var, let or const. Since I'm new to this, most of the explanation on the internet is pretty advanced to understand atm, so could anyone explain the differences for me very short short and easy, alternatively give me a link that explain it in pretty easy terms?

Thanks in advance!

(Edit: Thank you so much for the answers, rly appreciate it!)

Top answer
1 of 5
52
Using the function keyword creates a function declaration. Function declarations are hoisted to the top of the scope, thus the following code works fine: add(1, 2) function add (a, b) { return a + b } There are readability advantages to using the function keyword as it clearly communicates that this is a function. We can also create an anonymous function expression and, because functions in JavaScript are first class citizens, assign a function to a named variable with let, const or var. Keep in mind though, variables declared with let and const are hoisted, but not initialized with a value. Whereas variables declared with var are hoisted and initialized with the value undefined. So because add is invoked prior to it's initialization in the code below we encounter an error. With let/const: add(1, 2) // Reference error - cannot access 'add' before initialization const add = function (a, b) { return a + b } With var: add(1, 2) // Type error - 'add' is not a function (as it is initialized as undefined when hoisted) var add = function (a, b) { return a + b } If you're assigning a function to a variable I can't think of any situation where you wouldn't use const. Functions should always be pure and immutable whenever possible. Given a certain input, the output should always be consistently the same. Using const protects you from accidentally reassigning a variable containing a function expression. Compare this to use of the function keyword, which allows you to declare a named function twice, overiding the previous: function add (a, b) { return a + b } function add (a, b) { return a - b } add(2, 1) // 1 This is a disadvantage of function declarations versus expressions. Provided you understand the pros/cons and how hoisting works, it is really personal choice whether you prefer function declarations versus expressions.
2 of 5
22
All you need to remember is that var should never be used in modern JavaScript. Functions should either use the function keyword or const (with arrow functions). Objects/arrays should always be const. Just always use const until you can’t.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-and-let-in-javascript
Difference between var and let in JavaScript - GeeksforGeeks
In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable outside the block. With the introduction of ES6 in 2015 two more keywords, let and const came into the picture.
Published   July 11, 2025
🌐
Sentry
sentry.io › sentry answers › javascript › difference between `let` and `var` in javascript
Difference between `let` and `var` in JavaScript | Sentry
Variables declared by let are only available inside the block where they’re defined. Variables declared by var are available throughout the function in which they’re declared. Consider the difference between these two JavaScript functions:
🌐
Reddit
reddit.com › r/learnjavascript › preference reason : var vs let
r/learnjavascript on Reddit: Preference reason : var vs let
August 23, 2021 -

I am completely new to JavaScript. Just started exploring different aspects of ES6. What I noticed is, most of my seniors who has worked and implemented JavaScript in many of their projects, almost all of them suggested to use the keyword "let" instead of "var" while working with a variable declaration. Can anyone give me some insight for the preference of let over var? Thanks in advance.

🌐
Medium
medium.com › @singhshweta › let-vs-var-an-underestimated-difference-in-js-world-51a3f5641b81
let vs var : An underestimated difference in JS world | by Shweta Singh | Medium
February 3, 2022 - var and let are both used for variable declaration in JavaScript but the difference between them is that var is function scoped and let is block scoped. It can be said that a variable declared with var is defined throughout the program as compared ...
🌐
Test-yourself
test-yourself.com › articles › show › 5e06a8026c79c5001dfc903b › JavaScript - let vs var
JavaScript - let vs var
The most noticeable difference between let and var is their scope. Before the ES6 specification, there were only two types of scope in the JavaScript: local and global.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
The variable total is declared with the let keyword. The value of total can be changed. Undeclared variables are automatically declared when first used: ... It's a good programming practice to declare all variables at the beginning of a script. The var keyword was used in all JavaScript code before 2015.
🌐
Codedamn
codedamn.com › news › javascript
Difference between let and var in JavaScript?
January 15, 2023 - In JavaScript, the scope of a variable determines where in your code you can access it. Variables declared with var are either globally scoped or function scoped, while variables declared with let are block scoped.
🌐
DEV Community
dev.to › jps27cse › difference-between-var-let-and-const-in-javascript-53c
Difference between var, let and const in JavaScript - DEV Community
September 20, 2023 - Use let and const instead of var when writing JavaScript code to prevent unexpected scope problems and create more predictable, maintainable code. If you need to reassign a variable, use let; if you wish to declare constants, use const.
🌐
Favtutor
favtutor.com › articles › let-and-var-difference-javascript
Differences Between let and var in JavaScript Explained
December 23, 2023 - In conclusion, understanding the differences between let and var is crucial for effective variable declaration in JavaScript. While var has been the traditional way of declaring variables, let provides block scope and stricter behavior, making ...
🌐
NamasteDev
namastedev.com › home › article › all about let, const and var in javascript
All about let, const and var in Javascript - NamasteDev Blogs
June 15, 2024 - Use let and const instead of var when writing Javascript code it will help to prevent the unexpected scope problems and create more predictable, maintainable and cleaner code.
🌐
W3Schools
w3schools.com › js › js_let.asp
JavaScript Let
ES6 introduced the two new JavaScript keywords: let and const. These two keywords provided Block Scope in JavaScript: Variables declared inside a { } block cannot be accessed from outside the block:
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
September 1, 2024 - It is function-scoped, meaning a var variable is only accessible within the function where it is declared. If declared outside a function, it becomes global. Unlike let and const, which are block-scoped, var does not respect block boundaries (e.g., ...
🌐
Quora
quora.com › Why-should-I-use-var-let-in-JavaScript-I-am-new-to-JS-and-I-see-everyone-using-var-and-let-but-I-have-tried-creating-variables-without-var-or-let-and-it-worked-so-isnt-it-shorter-to-just-not-use-them
Why should I use var/let in JavaScript? I am new to JS and I see everyone using var and let, but I have tried creating variables without var or let and it worked, so isn't it shorter to just not use them? - Quora
Answer (1 of 3): Javascript is a very permissive language. It will manage your mistakes to some extent. That’s why you can forget to declare the variable. [code ]var[/code] is the original way of declaring variables in javascript. At some point the language evolved to allow the developers ...
🌐
Hacker News
news.ycombinator.com › item
JavaScript variables: var and let and const | Hacker News
March 9, 2020 - I know it can be confused with `var`, but no one should ever use that now that `let` exists · One of these days I'll write my own little language that compiles to TypeScript or something and fix that :)
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-let-and-const-keywords-in-javascript
Difference between var, let and const keywords in JavaScript - GeeksforGeeks
When using let in a loop, each iteration of the loop creates a new instance of the variable. This is different from var, which shares the same variable across all iterations. ... Trying to access i outside the loop causes an error since it’s ...
Published   January 16, 2026
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › let
let - JavaScript | MDN
Many style guides (including MDN's) recommend using const over let whenever a variable is not reassigned in its scope. This makes the intent clear that a variable's type (or value, in the case of a primitive) can never change.