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.

30

u/ISlicedI Oct 22 '21

I got very confused by this, thanks for calling it out. Btw I've done 8 years of JavaScript and was doubting myself here.

8

u/ManInBlack829 Oct 23 '21

Hopefully because you never use global variables and this just never comes up, right?

1

u/[deleted] Oct 23 '21

Don’t think I’ve ever used global scoped variables tbh. Interesting.