es2015
At Iron Yard Austin, we just learned how to use es2015 and compile our JS using Babel. Among the most useful changes is the ability to replace var with let and const. Here’s a quick breakdown of the differences:
var is standard JS and what we’re used to. It allows you to declare variables and change the value of those variables elsewhere in your code. Example:
var counter = 0;
counter = 5;
const lets you declare a constant whose value is declared once and cannot be changed elsewhere in your code. Example:
const someObj = {
color: red,
material: plastic
};
console.log(someObj);
let is a lot like var, in that you’re allowed to change the value of some variable you’ve declared. The benefit of using let is that it allows for block scoping. This means that the variable will be scoped to your block or function, rather than the global scope, which allows you to use the same variable name in multiple locations without any problem. Example:
someFunction (arr) => {Â
let thisThing = arr.length;
for (var i=0; i < thisThing; i++) {
  console.log(i);
};
anotherFunction (string) => {
let thisThing = string.length;
for (var i=0; i < thisThing; i++) {
   if ([i] === “bob”)
   console.log(i);
};
I recently refactored some of my code from my Instagram Clone assignment using const and let, as well as string templates and arrow => function notation. You can check out my es2015 updated code HERE.
















