-
-
Save airyland/4070694 to your computer and use it in GitHub Desktop.
more "mental tax": interpreting code that uses `let` in many nested blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
not terribly difficult to predict which statements print what. | |
*/ | |
function foo() { | |
var a, b, c, d; | |
if (true) { | |
if (true) { | |
a = 1; | |
if (true) { | |
b = 2; | |
c = 3; | |
if (true) { | |
d = 4; | |
console.log("d: " + d); | |
} | |
console.log("c: " + c); | |
} | |
console.log("b: " + b); | |
} | |
console.log("a: " + a); | |
} | |
} | |
// will print: | |
// d: 4 | |
// c: 3 | |
// b: 2 | |
// a: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
a little harder to trace visually to know what will happen. admit it: it takes some extra | |
visual and mental processing to track which scope each variable is in, and whether the | |
`console.log()` in each block will succeed or fail. | |
*/ | |
function foo() { | |
if (true) { | |
if (true) { | |
let a = 1; | |
if (true) { | |
let b = 2, c = 3; | |
if (true) { | |
let d = 4; | |
console.log("d: " + d); | |
} | |
console.log("c: " + c); | |
} | |
console.log("b: " + b); | |
} | |
console.log("a: " + a); | |
} | |
} | |
// will print: | |
// d: 4 | |
// c: 3 | |
// b: undefined | |
// a: undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment