I'm new to the JavaScript World and I'm confused after I knew a lot about the global object (usually window) and knew it's just an object like any object I'd create and that var set properties in the global object unlike let
after that Is there any difference between window.a = something (like in any object) and var a = something?
In the global context, using
var
vs assigning towindow
are indeed quite similar. However, yes there are a number of differences. Here are a few I can think of:var
declarations are hoisted, which means that you can use a variable declared withvar
before you've declared it. On the other hand, trying to use something assigned towindow
before that assignment has occurred will produce aReferenceError
:var
cannot be removed from the global object, but simple assignments towindow
can be removed:var
declarations are scoped to the current execution context. In the global scope, this doesn't differ from assigning towindow
, but within a function, avar
will disappear when the function returns.