ES2015 - let and const

Some useful information I have found on the new let and const functionality. I have started using this and converting it with babel to es3 however this loses all the nice features its provided. But at least its future proofing.

let

You can only declare each let name once.

let dog = "harvey";

Temporal Dead zone

You can not call a let before its been created

function doSomething(){
    console.log(dog);  // Logs undefined
    let dog = "Harvey";
    console.log(dog); // Logs harvey
}

Let creates a block scoped variable

  let dog = "Harvey";

  if(true) {
    let dogNew = "Rosie";  
  }

  console.log(dog); // logs Harvey
  console.log(dogNew); // logs Not defined as the let is inside a block

Let in for loops

When using let in a for loop its not accessible outside the loop and is scoped to the block. The same principle as above.

  for(var i = 0; i < 10; i++){
    console.log(i) // Logs 0 - 9
  }

  for(let j = 0; i < 10; i++){
    console.log(j) // Logs 0 - 9
  }

  console.log(i) // Logs 0 - 9
  console.log(j) // Logs undefined as the let is inside a block

Const

You can’t reassign to a const variable. Its scoped the same as let lexically inside curly braces.

  const dog = "harvey";
  dog = "rosie"; // You cant reassign to a const