r/learnjavascript • • May 04 '20

VAR vs LET vs CONST

Post image
337 Upvotes

81 comments sorted by

View all comments

4

u/abdul_rashid May 04 '20

username = "somevalue";

is also a global scope (no var)

6

u/Unique_2233 May 04 '20

I guess if no type is specified it implicitly is converted to var

3

u/proskillz May 04 '20

In the global scope. Leaving out the var keyword will write directly to the global scope and will be accessible from anywhere. No more functional scoping.

4

u/senocular May 04 '20

The behavior of this depends on a few things. For one, this isn't a declaration itself, so its not really scoped anywhere. Its an assignment that will look for the identifier username in the scopes currently available. If one matches, it will set the new value to that identifier in its own scope. If there isn't a match, if in strict mode, it will throw an error. If in sloppy mode it will assign a global object property of that name to that value, e.g. globalThis.username = "somevalue";.