JettTech
12/6/2017 - 8:07 PM

Const vs Let : ES6

  • The question to ask when deciding between using LET or CONST, is whether the var is going to be REASSIGNED or NOT.
  • IF the variable WILL be reassigned a value inside the block, then should use LET, if not, then use CONST.
let name = "Nick"; 

const tinyize = function (name) {
  const myName = 'Tiny ' + name + '!';
  return myName;
};

name = tinyize('Rick');
console.log(name);// Tiny Rick

for (let i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i); // let = 0 let = 1, let = 2, let = 3, let = 4
  }, 100);
}

console.log(i) //>>> THIS WILL create an ERROR, because LET is block scoped!! (vs Var that is instead scoped to the Lexical Feild).