var, let, or const


1. var

The result is 3.

Var is very generous. It can declare several times the same variables name.


var: Global scope example

The result is 1.

a is declared out of function. So it is a global scope. Therefore a can read in function.


var: Local scope example

The result is Uncaught ReferenceError: b is not defined.

a is declared in the function. It is a local scope. Therefore b works only in function block, not out of function.


2. let

The let statement declares a block scope local {} variable, optionally initializing it to a value.

The result is like below:


3. const

const can use only once.

The result is like below:

When JS variables define, the best practice is const use 99%: Immutability and pure function are so important.

Except when variables should have to change, use let like below:

for (let i = 0; i > 100; i__)
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.