Introduction to React
In ES6 we
const represents a constant.
let can be used for declaring a variable though there is one fundamental difference between them.
If you try declaring a variable with a name that's already taken using the
var keyword, the older variable will be overridden:
The above code will output
World which indicates that the older value was overridden. However if we try to do something similar using the
It will show an error at the second line because a variable called ‘myVariable’ is already declared.
Another difference between
let is that
var has a function scope while
let has a block scope. Let's look at the example:
The above code will have the output
7, which means that the above code is equivalent to:
Let's look at the example with the
If we run the above code it will show an error in the last line since the
i variable is not declared in the scope where it's being referenced at.