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.

Redeclaration:
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.

20 Likes

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?

console.log(k);
const k="figure";

Error: Reference Error k is not defined

console.log(k);
let k="figure";

Error: Reference Error k is not defined

console.log(k);
var k="figure";

O/p:Undefined

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

I found an article:

https://www.geeksforgeeks.org/difference-between-var-and-let-in-javascript/

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.

4 Likes

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…

https://mathiasbynens.be/notes/es6-const

2 Likes

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.

4 Likes

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.

1 Like

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
  }

  bar();

  console.log(x); // 1
}

foo();
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

2 Likes