JS Window Object
Written By: Avinash Malhotra
Updated on
Window
JS Window Object is the global object in JavaScript. For a web browser, window object tell information about current browser window. Window Object is also the global object of browser environment.
window object is available in browser environment only. In other environment's, there is no window object. For exp, in Node JS, global
is used instead of window
object.
We can also use globalThis, a common object in modern JS for both frontend and backend.
All global variables and functions are windows properties and methods. See Example
var x="hello js";
var y=function(){ return "hi"};
window.x; // return "hello js"
window.y(); // return "hi"
But Block scope const
and let
are not windows properties and methods.
let x="hello js";
let y=function(){ return "hi"};
const pi=Math.PI;
window.x; // return undefined
window.y; // return window.y is not a function
window.pi // return undefined
Window Properties
Some common properties of Window Object.
Property | Details | Use |
---|---|---|
innerWidth | width of window | |
innerHeight | height of window | |
devicePixelRatio | ratio of physical resolution by actual resolution |
Window Methods
JS Window Methods or build in functions.
Method | Use |
---|---|
window.open() | Open a new browser window. For exp
|
window.close() | Close browser window opened by window.open method. For exp
|
window.print() | Print current browser window. Exp
|
window.resizeTo() | resize browser to parameter inside. For exp window.resizeTo(600,400) will resize window to 600 by 400.
|
window.moveTo | move window to left top corner of screen |
Get all window Properties and methods
To get all windows properties and methods for current browser, we can use for in loop on window object. Most of the properties of windows objects are same on all browsers, but some may not exists on another browser. The reason is different browser engine.
for( let i in window){
console.log(i, window[i]);
}
Output of loop in table
S No | Property | Value |
---|