JavaScript Window

Beginner
8 min read
Updated: Nov 2026
3 examples
Browser tab

Definition and Usage

In a browser, window is the object for the current tab. It is the global object, so alert("Saved") and window.alert("Saved") call the same function. window.document is the page. window.location is the URL.

Some members are properties you read: innerWidth, closed, location.href. Others are methods you call: alert(), setTimeout(), open(). setTimeout returns a number in the browser. Pass that number to clearTimeout if you need to cancel the timer.

This page teaches what window is. The Window Methods index and Window Properties index list every method guide and property in this JavaScript tutorial.

Read a property. Call a method.

window.innerWidth is a number. window.alert("Saved") is a function call. window.innerWidth() throws.

JavaScript Syntax

Common ways to use the window in a browser script:

JavaScript
window.alert("Saved");           // method, returns undefined
const id = window.setTimeout(fn, 1000);
window.clearTimeout(id);

const url = window.location.href; // property, a string
const wide = window.innerWidth;   // property, a number
const same = window === window.self;

Core Ideas

Ideas every JavaScript window tutorial builds on:

IdeaExampleRemember
Globalwindow === selfThe tab is the global object
Pagewindow.documentThe DOM lives on document
URLlocation.hrefA string, no parentheses
SizeinnerWidthViewport width in CSS pixels
Dialogalert("Saved")Blocks until OK, returns undefined
TimersetTimeout(fn, 1000)Returns an id you can cancel

JavaScript Examples

Three short browser programs. The output is what those lines print in a tab.

Example 1 — The tab is the global object

self is this window. alert is a function on it.

JavaScript
console.log(typeof window);
console.log(window === window.self);
console.log(typeof window.alert);

How It Works

  1. typeof window is "object" in a browser tab.
  2. window.self is the same object, so the comparison is true.
  3. alert is a method, so its type is "function".

Example 2 — Read properties, do not call them

The URL is a string. The viewport width is a number. This page is still open.

JavaScript
console.log(typeof window.location.href);
console.log(typeof window.innerWidth);
console.log(window.closed);
Properties index

How It Works

  1. location.href is the full URL, so its type is "string".
  2. innerWidth is the viewport width in CSS pixels, so its type is "number".
  3. closed is false while this tab is still open.

Example 3 — Cancel a timer with its id

Schedule a log, then cancel it before it runs. The delayed line never prints.

JavaScript
const id = window.setTimeout(function () {
  console.log("later");
}, 1000);

console.log(typeof id);
window.clearTimeout(id);
console.log("canceled");

How It Works

  1. setTimeout schedules "later" and, in the browser, returns a numeric id.
  2. clearTimeout(id) cancels that call, so "later" is not printed.
  3. The last log prints canceled immediately.

Next: Methods and Properties Indexes

After you know what the window is, open the indexes for the rest of this JavaScript tutorial.

Look upWhy
alert()Show a message and wait for OK
setTimeout()Run a function once after a delay
open()Open another tab and keep a reference
innerWidthRead the viewport width

Full lists: Window Methods index and Window Properties index.

Frequently Asked Questions

In a browser, window is the global object for the current tab. alert(), setTimeout(), and the page URL all hang off it. window.document is the page. window itself is the tab.
Both call the same function. alert is a property of window, so a browser script can omit window. Writing window.alert("Saved") makes the object explicit in a tutorial.
A property is a value you read: window.innerWidth, window.closed, window.location.href. A method is a function you call: window.alert("Saved"), window.setTimeout(fn, 1000). Adding parentheses to innerWidth throws.
No. In the browser it returns a number, the timer id. Pass that id to clearTimeout to cancel the call. The function you scheduled runs later, unless you cancel it first.
Practice the examples here, then open the Window Methods index for alert(), setTimeout(), and open(). The Window Properties index lists innerWidth, location, document, and the rest.

Browse every window method

Open the index for alert(), setTimeout(), open(), and the rest.

Window Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful