Last active
February 27, 2021 17:57
-
-
Save techsin/bfd32b73dcf2019ac59cda42b41e5039 to your computer and use it in GitHub Desktop.
scope closure
This file contains hidden or 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 = ['a','b', 'c'] | |
for (let i = 0; i < 3; i++) { | |
var c = a[i]; | |
setTimeout(()=> console.log(c)); // c c c | |
} | |
//is same as, var is function scoped | |
var c; | |
a = ['a','b', 'c'] | |
for (let i = 0; i < 3; i++) { | |
c = a[i]; | |
setTimeout(()=> console.log(c)); // c c c | |
} | |
//let is block scoped | |
a = ['a','b', 'c'] | |
for (let i = 0; i < 3; i++) { | |
let c = a[i]; | |
setTimeout(()=> console.log(c)); // a b c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment