r/learnjavascript • • Oct 22 '21

Difference between var, let and const.

Post image
430 Upvotes

49 comments sorted by

View all comments

110

u/ex-igne-vita Oct 22 '21

var is not "global scoped" unless it's declared in the global scope... and if let or const are declared in the global scope then they're "global scoped" too.

If you want a variable to be global scoped, don't declare it at all, just define it.

2

u/justhereforlife Oct 22 '21

To add on to your point, if you omit the var (assuming not in strict mode) won't a global scoped variable be created? I don't believe the same is true of let/const. Could that be what the graphic is trying to convey?

Question marks because Im not certain if what I'm saying is actually the case haha

4

u/mareksl Oct 22 '21

Well, if you omit the var, what will be the difference between that and omitting the let/const?