In learning ES6 the first we learn is to declaring variables.
ES6 dictates the use of let and const in declaring variables. Out of all the developer videos and articles 3 simple rules emerge in using var-let-const
- var is totally discouraged in ES6 specification.
- Everything and all variables are to be declared using const by default.
- Use let if value of the variable changes.
try the below snippets in your editor
if(true){
var someVariableName = "hello world"
}
console.log(someVariableName) //leaks outside the if block, var is not block scoped - :(
Now, try the below code.
if(true){
let someVariableName = "hello world"
}
console.log(someVariableName) //does'nt leak outside.Infact the code will give an error.
const
is for mostly declaring variables which never change their value.