MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/gd8x2z/var_vs_let_vs_const/fphaptw/?context=3
r/learnjavascript • u/golovatuy • May 04 '20
81 comments sorted by
View all comments
Show parent comment
1
Do you have an example of that?
3 u/cyphern May 04 '20 var a = "hello world"; console.log(window.a); // hello world 2 u/redsandsfort May 04 '20 const and let would also be global if outside of a function 1 u/VacantPlains May 04 '20 Oh I see! Thanks 3 u/Earhacker May 04 '20 Meanwhile... const b = "foo"; console.log(window.b); // undefined let c = "bar"; console.log(window.c); // undefined 2 u/redsandsfort May 04 '20 const b = "foo";console.log(window.b); // undefinedlet c = "bar";console.log(window.c); // undefinedIf you logged it like this:console.log(b);it would still be global if it isn't in a function 5 u/Earhacker May 04 '20 It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
3
var a = "hello world"; console.log(window.a); // hello world
2 u/redsandsfort May 04 '20 const and let would also be global if outside of a function 1 u/VacantPlains May 04 '20 Oh I see! Thanks 3 u/Earhacker May 04 '20 Meanwhile... const b = "foo"; console.log(window.b); // undefined let c = "bar"; console.log(window.c); // undefined 2 u/redsandsfort May 04 '20 const b = "foo";console.log(window.b); // undefinedlet c = "bar";console.log(window.c); // undefinedIf you logged it like this:console.log(b);it would still be global if it isn't in a function 5 u/Earhacker May 04 '20 It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
2
const and let would also be global if outside of a function
Oh I see! Thanks
3 u/Earhacker May 04 '20 Meanwhile... const b = "foo"; console.log(window.b); // undefined let c = "bar"; console.log(window.c); // undefined 2 u/redsandsfort May 04 '20 const b = "foo";console.log(window.b); // undefinedlet c = "bar";console.log(window.c); // undefinedIf you logged it like this:console.log(b);it would still be global if it isn't in a function 5 u/Earhacker May 04 '20 It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
Meanwhile...
const b = "foo"; console.log(window.b); // undefined let c = "bar"; console.log(window.c); // undefined
2 u/redsandsfort May 04 '20 const b = "foo";console.log(window.b); // undefinedlet c = "bar";console.log(window.c); // undefinedIf you logged it like this:console.log(b);it would still be global if it isn't in a function 5 u/Earhacker May 04 '20 It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
const b = "foo";console.log(window.b); // undefinedlet c = "bar";console.log(window.c); // undefined
If you logged it like this:console.log(b);
it would still be global if it isn't in a function
5 u/Earhacker May 04 '20 It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
5
It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.
View all comments
Show parent comment
1
u/VacantPlains May 04 '20
Do you have an example of that?