Monday, February 6, 2017

ES6 Variables - 1


var can be updated.
They can be redefined.

var width=100;
var width=200;
doesn’t warn us. We are creating 2 variables with the same name, so the first one gets over-written.

var are ‘function scoped’.
They are only available inside the function they are created.
If they are not inside any function, they are globally scoped in window.

Because var is function scoped, it can leak out of a global if statement because there is no function. 
if(age>12){var dogYears=100;}
console.log(dogYears); //this works
let and const however, are ‘block scoped’.
They don’t leak out of {}
if(age>12){let dogYears=100;}
console.log(dogYears); //this doesn’t work
functions are also blocks. So they don’t leak out of a function.

var
*doesn’t warn us
*leaks in global


No comments:

Post a Comment