ES6 cheatsheet β€” Variable Declarations

Headshot Mihai Serban

Serban Mihai / 16 October 2018

~1 min read

es6-variables

ES6 brought let and const with proper lexical scoping. let is the new var. Constants work just like let, but can’t be reassigned. let and const are block scoped. Therefore, referencing block-scoped identifiers before they are defined will produce a ReferenceError.

Example using var:

var variable = 5;

{
  console.log('inside', variable); //5
  var variable = 10;
}

console.log('outside', variable); //10

Example using const:

const variable = 5;

variable = variable*2; // TypeError: Attempted to assign to readonly property.

Constants are tricky with array and objects. The reference becomes constant but the value does not.

const variable = [5];

console.log(variable) // [5]

variable = [2]; //TypeError: Attempted to assign to readonly property.

variable[0] = 1;
console.log(variable) // [1]

You can find a more complete ES6 cheetsheet on my Github page.

P.S. If you ❀️ this, make sure to follow me on Twitter, and share this with your friends πŸ˜€πŸ™πŸ»

gatsby  +  contentful  +  netlify  = ❀️[Source code]