[ES2015] var, let and const
var, let and const
var is an issue because
var is hoisted.
var is function scope,
let is block scope.
let is the new
const is a constant. it is for something that is only going to be defined once. You can not redefine
const is not exactly immutable, the values in
const can sometimes be changed, for example if it’s an object and you want to chaneg property values inside, you can. But you can reassign that whole object again.
const will never be reassigned, but it can change. (e.g. Arrays and Objects)
- You should always use
const if you do not really need to change the variable. You should minimize mutable state
- There is comfort in knowing that something will never change.
- the problem with
var is the scope.
let aims to resolve that with local scope.
var is not recommended because of it’s scoping issues.