Difference between var, let and const?

Hi all. I was wondering about difference between var, let and const. For example with var:

var myVar;
var globalVariable = "I am a global variable!";

Then let:

let myVar;
let localVariable = "I am a local variable!";

And const:

const myVar;
const immutableVariable = "I am an immutable variable!";

Please answer as soon as possible. :smile:

Thank you!
Pummarin :smile:

Thereā€™s actually a great article on this published by freeCodeCamp that you can find here:

https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

1 Like

Hey smart people!I have a doubt regarding 2nd lessonā€™s content in ES6 module of JAVASCRIPT. Can you tell the the difference between the below two programs.

var numArray = [];
for (var i = 0; i < 3; i++) {
  numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
In the first code,var inside the "for" loop right so i thought it was a local variable so it wouldn't return the value of i out of the "for" block.

In the first code,var inside the ā€œforā€ loop right so i thought it was a local variable so it wouldnā€™t return the value of i out of the ā€œforā€ block.

var numArray = [];
var i;
for (i = 0; i < 3; i++) {
  numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
```But over here var is declared globally and the result is same as i expected .

How is the result same when one var is declared locally  and  another globally.
I am confused with the first program.
Thanks in advance for taking your valuable time and answering.

HI sir!I have a doubt regarding 2nd lessonā€™s content in ES6 module of JAVASCRIPT. Can you tell the the difference between the below two programs in terms of console.log(i).

var numArray = [];
for (var i = 0; i < 3; i++) {
  numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
In the first code,var inside the "for" loop right so i thought it was a local variable so it wouldn't return the value of i out of the "for" block. console.log(i) should return an error RIGHT?

In the first code,var i inside the ā€œforā€ loop right so i thought it was a local variable so it wouldnā€™t return the value of i out of the ā€œforā€ block.

var numArray = [];
var i;
for (i = 0; i < 3; i++) {
  numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
```But over here var i is declared globally and the result is same as i expected .

How is the result same when one var is declared locally  and  another globally.
I am confused with the first program.
Thanks in advance for taking your valuable time and answering.

In the the first

note that num numArray has already been decleared globally, the for loop does the job of modifying the array content.

In addition i is decleared with var keyword which makes it visible to the main scope of the program

in the second, variable i remains visible since itā€™s decleared with var keyword too.

Bottom line, irrecpective of scope, when ever var keyword is used the variable decleared become visible to all scope

Hey! sorry if i didnt mention i was talking about console.log(i);According to me the console.log(i) should return an error in the first program since there it is a local variable.

Hello there,

There is very little difference between your two examples. Here is a more in-depth article: https://wesbos.com/for-of-es6

It is just the way var behaves

1 Like