Skip to content

Instantly share code, notes, and snippets.

@ktajpuri
Created February 5, 2019 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktajpuri/c8ae7e0c548cffd69da9c60ec11478e8 to your computer and use it in GitHub Desktop.
Save ktajpuri/c8ae7e0c548cffd69da9c60ec11478e8 to your computer and use it in GitHub Desktop.
// Say you have this function
function demo() {
var a = 1;
let b = 2;
if (1) {
var c = 3;
let d = 4;
console.log(a, b, c, d);
}
console.log(a, b, c, d);
}
// if we execute demo() it will give a ReferenceError: d is not defined. This is because d being defined by let wont be available
// outside the scope of if. If we define it with var it works fine. How does babel make us get this reference error for the first case?
// We need to define the block within an IFFE
// This works fine
function demo() {
var a = 1;
let b = 2;
if (1) {
var c = 3;
var d = 4;
console.log(a, b, c, d);
}
console.log(a, b, c, d);
// This will give e refereence error for d as it would give if d was defined with let.
function demo() {
var a = 1;
let b = 2;
if (1) {
var c = 3;
(function() {
var d = 4;
console.log(a, b, c, d);
})();
}
console.log(a, b, c, d);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment