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

🌐
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.

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>

🌐
Sentry
sentry.io › sentry answers › javascript › difference between `let` and `var` in javascript
Difference between `let` and `var` in JavaScript | Sentry
A var variable will be available thoroughout the function body in which it is defined, no matter how deeply nested its definition. A let variable will only be available within the same block where it is defined.
🌐
DEV Community
dev.to › chintanonweb › mastering-javascript-variables-a-deep-dive-into-let-var-and-const-866
Mastering JavaScript Variables: A Deep Dive into let, var, and const - DEV Community
April 23, 2024 - It's generally recommended to use let and const instead for clearer code semantics and better scoping. A: No, variables declared with const cannot be reassigned after initialization.
🌐
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 to let. Sometimes using let makes it easy and can escape us from using closures if needed.
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
Use let for Reassignment: Opt for let when you know a variable’s value will change, such as in loops or conditions. It combines flexibility with block-scoping, reducing the risk of variables leaking out of their intended scope.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-and-let-in-javascript
Difference between var and let in JavaScript - GeeksforGeeks
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.
Published   July 11, 2025
Find elsewhere
🌐
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.
🌐
Appsmith
community.appsmith.com › content › guide › variables-javascript-comprehensive-guide-var-let-and-const
Variables in Javascript: A Comprehensive Guide to Var, Let, and Const | Appsmith Community Portal
August 11, 2023 - Because of these peculiarities, ... less common in modern JavaScript, and it's often recommended to use let or const instead for clearer scoping rules and better maintainability....
🌐
W3Schools
w3schools.com › js › js_let.asp
JavaScript Let
You can not accidentally redeclare a variable declared with let.
🌐
Zipy
zipy.ai › blog › difference-between-let-and-var-in-javascript
difference between let and var in javascript
April 12, 2024 - Another important difference is how let and var are hoisted. Hoisting is JavaScript's default behavior of moving declarations to the top of their scope before code execution.
🌐
Quora
quora.com › In-JavaScript-when-should-I-use-let-instead-of-var-when-initializing-variables
In JavaScript, when should I use 'let' instead of 'var' when initializing variables? - Quora
Answer (1 of 6): var — function scope let — block scope var used to work by the way of ‘hoisting'. Which means wherever var was used in the function () , irrespective of the location, the variables used to get declared at beginning of the function() by JavaScript, and at the actual line of decl...
🌐
DEV Community
dev.to › shameel › javascript-variable-should-you-choose-let-var-or-const-47ke
JavaScript Variable: Should you choose let, var or const? - DEV Community
November 2, 2023 - It's best to use let or const instead. Use let when you need to reassign the variable's value within the same block. Use const when the value should never change after initialization.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Var VS let keywords - JavaScript - The freeCodeCamp Forum
January 15, 2019 - In the first ES6 challenge it explains that let was added because var can accidentlly be overwritten without causing any errors. My question: why didn’t they reverse it in ES6? To me, it seems like “let” is less permanent language than “var”… like “ohhh I will let i = 5 for now, but later I might let i = 6” Basically, what I’m getting as is I think it would make more sense if let was the one that could be overwritten and not var.
🌐
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 intent...
🌐
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.
🌐
Programiz
programiz.com › javascript › let-vs-var
JavaScript let Vs var (with Examples)
To learn more, visit JavaScript let browser support. Note: In case of global scope, both var and let will behave in the same way.