Block scope level variable let Declaring and hoisting – ES6

let declaration and usage

The let statement allows to create and initialize variables inside the block scope. This might be function, statements (if, for, etc) or expression. By default a declared yet not initialized variable has undefined value like as var declaration.

let is introduced in ECMAScript 6, which allows to keep the code more encapsulated on a block level.

Try in JS Bin

let Hoisting

Variables declared with let are hoisted to the top of the block. But when the variable is accessed before declaration, JavaScript throws an error: ReferenceError: <variable> is not defined.

Try in JS Bin

Redeclaring the same variable within the same function or block scope raises a SyntaxError.

 

Leave a Reply

Your email address will not be published. Required fields are marked *