What is the difference between `let` and `var` when declaring and what about reassigning?

So can I reassign variables using let just one time or several times?

I think it is a some kind of very confusing mistake there:

The var keyword is used in pre-ES6 versions of JS.
let is the preferred way to declare a variable when it can be reassigned,

Actually it is not true about the difference between let and var.
There’s a difference in scope, but not in reassignment. Reassigned could be variables declared by any non-const keyword. That’s the definition of variable. It can be reassigned by using assignment operator.

If they meant redeclared, it works only in strict mode and is just the polar opposite.

Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not:

‘use strict’;
let me = ‘foo’;
let me = ‘bar’; // SyntaxError: Identifier ‘me’ has already been declared
‘use strict’;
var me = ‘foo’;
var me = ‘bar’; // No problem, me is replaced.


Just completed the Variable assignment, and decided to switch to the forum for some additional questions.

So, the keyword let is not replacing var, right? You are saying that even though the ‘purpose’ is the same, it alters when applying ‘use strict’ directive? And, if it’s not too much to ask (newbie question), when would I have to, or when should I, use the ‘use strict’ directive?

const k="figure";

Error: Reference Error k is not defined

let k="figure";

Error: Reference Error k is not defined

var k="figure";


Explain this phenomenon what’s the difference using let & const throwing error .using var code is executed .

I found an article:


which shows an example similar to yours, but doesn’t explain it perfectly.

let and const where introduced in an update (es6, also know as emcascript 2015), and are an improvement.

the error is certainly better, if the program just continues with undefined, the program might crash later and its more difficult to debug, while if an error is thrown, you instantly know where the problem is.


Everything made sense to me except one thing. Why would one use const in any situation when you could just use let?

Not only are you able to change the value of let but it does the same thing as const. Why not just use let all the time in case you do need to change it.

You might find this article an interesting read…



What i dont understand is Let also cant be reassigned in the same scope.

let x = ‘test’;
let x = ’ test2’;

console.log (x) // SyntaxError: Identifier ‘x’ has already been declared

let x = ‘text’;
x = ‘text2’;

console.log (x); // test2

what is the difference here?

re-assigning and declaring a new variable with the same name, are two different things.


That was pretty interesting. Thanks for the link.

1 Like

With let x = ‘test’ you have already assigned the value to ‘x’ once. Thus, the variable name has already been defined in the system and cannot be declared again.

It’s like giving the same variable name 2 values and confusing the system which one to pick. Hope it helps.


The let keyword was introduced in ES6 (a version of JavaScript announced in 2015) and is basically an improved version of the var keyword. Variables declared with var are function scoped while variables declared with let are block scoped.

This means that variables declared with var are only accessible within their containing functions and their child functions:

function foo() {
  var x = 1;

  function bar() {
    console.log(x); // 1


  console.log(x); // 1

console.log(x); // ReferenceError: x is not defined

and variables declared with let are only accessible within their containing block and their child blocks:

if (condition) {
  let x = 1;
  if (condition) {
    console.log(x); // 1

  console.log(x); // 1

console.log(x); // ReferenceError: x is not defined

The reason let was introduced in ES6 was because function scope was confusing and error-prone.

When creating a variable with the var keyword, it will become a property of the window object, but when creating a variable with the let keyword it will not be a property of the window object

var x = 1;
let y = 1;

console.log(window.x); // 1
console.log(window.y); // undefined

There are some other slight differences such as hoisting and closures but these are the main differences

So basically, let is an improved version of var. Most of the times, you would use let


I saw scope word when compare let and ver. But I don’t clearly understand what is scope?

Enter a classroom. Declare something. Only the people in that classroom will hear that declaration and have any knowledge of its expression.

That is scope. The environment where a variable is declared, also known as a namespace. If we write something on the blackboard in that classroom, it is confined to that blackboard. The classroom is akin to a namespace. It’s the place that inscription can be found.

That classroom is part of a wing which is part of a building which is part of a campus. Therein we have built a chain of various namespaces, known as the scope chain. It gets confusing when we try to venture to another classroom and see if we find that inscription on the board. Under scope chain architecture this would never happen. All those rooms are linked to the same link in the chain, the wing. We cannot traverse sideways on the scope chain, only upwards.

From the classroom we move up the chain to the wing, then to the building, then to the campus. Abstract, I know, but think on it.

1 Like