1 minute read
Excusing the horrific pun, it's great to see JavaScript support more options when it comes to variable declaration. Let's go over them together and then it'll be easier for me to remember the nuances of each later on.
The old standby. var can place variables in either the global scope or a function's scope. var is subject to hoisting!
var x = 10;
New to ES2015. const stands for constant and it will only allow you to set a variable one time. This property of immutability does not work on objects or arrays (since arrays in JavaScript are just a type of object).
const YEAR = 2018;
It's convention to capitalize all the letters of a constant variable.
New to ES2015. let allows you to establish the block scope of a variable and it has a few special rules:
let x = 10;