ES6 syntax changes
ES6 brings in significant syntactic changes to JavaScript. These changes need careful study and some getting used to. In this section, we will study some of the most important syntax changes and see how you can use Babel to start using these newer constructs in your code right away.
Block scoping
We discussed earlier that the variables in JavaScript are function-scoped. Variables created in a nested scope are available to the entire function. Several programming languages provide you with a default block scope where any variable declared within a block of code (usually delimited by {}
) is scoped (available) only within this block. To achieve a similar block scope in JavaScript, a prevalent method is to use
immediately-invoked function expressions (IIFE). Consider the following example:
var a = 1; (function blockscope(){ var a = 2; console.log(a); // 2 })(); console.log(a); // 1
Using the IIFE, we are creating a block scope for the a
variable. When a variable...