The window
object is effectively two things:
The global object for browser-based JavaScript. All of the native objects and methods (Array
, String
, setTimeout()
) and anything you declare outside of any function's scope goes in the window
object. To test this, try opening a JavaScript console and checking this out:
window.String === String
// Returns true
The window
object also deals with the browser window. window.innerWidth
is the window's width; window.onresize
is a function that is triggered on the window resize. Because it's the "topmost" object, you can also say things like innerWidth
to get the window's width.
In general, it's a good practice to refer to write window.location
instead of just location
. Even though they'll work a lot of the time, you'll sometimes run into situations like this (which you don't want!):
function something() {
var location = 'the moon';
location.reload(); // Should be window.location.reload()
}
In the above example, you might've meant to refresh window.location
instead of the location
scoped inside this function.
And that's the window object!