The function body is hoisted along with function declaration. In the code snippet, the function declaration is moved to the top of scope through hoisting (above return statement). The equivalent code interpreted would be:
The assignment part of variable and function expressions are not hoisted, but only the declarations are!
The equivalent code interpreted would be:
What's new in ECMAScript 2015 (ES6)?
The variables declared with let or const keyword have the block in which they are declared, as well as any continued sub-blocks as their scope.
Hoisting in ES6
In ECMAScript 2015, let and const keyword variable declarations will hoist the variable to top of the block. However, referencing the variable in the block before variable declaration results in 'ReferenceError'. The difference between var/function declarations and let/const declarations is in the initialization portion. The former are initialized with undefined or generator function right when binding is created at top of the scope, while the latter stay uninitialized until let/const statement is run. The variable is called to be in a "temporal dead zone" from start of the block until the declaration is executed.
Variable declaration keywords: 'var' v/s 'let'
This happens due to block-level scoping of let variable declaration.
This article was originally posted on medium
Are you new to Linux and wants to do Linux Command Line? Get my Udemy course on "Linux Command Line for Beginners" at a discount price (limited offer) by clicking here.