Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- It could be a bit strange, but when I first time heard about ES6 block scoping, I felt very excited ;)
- Usually, for doing this I could use an immediately invoked function expression (IIFE).
- */
- // Like this
- (function() {
- // Doing stuff here
- })()
- // But now, we can use just '{}' for doing that!
- var a = 1;
- {
- let a = 2;
- console.log(a) // 2
- }
- console.log(a) // 1
- // Anyway, if we would need an async/await block scope, we would still use an IIFE:
- (async function(){ await ... })()
- // Another cool stuff which I would like to share is let keyword inside a block scope.
- // Let's assume we have a block scope with a var and let declared variables:
- {
- console.log(a); // 1
- console.log(b); // ReferenceError: b is not defined
- var a = 1;
- let b = 2;
- }
- /* This example shows that accessing let-declared variable earlier than its declaration causes an error,
- whereas var-declared orderind doesn't matter!
Add Comment
Please, Sign In to add comment