JavaScript Document defaultView Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

In browsers, document.defaultView returns the window object associated with a document, or null if none is available (MDN). The property is read-only.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.defaultView. Start with this line.

JavaScript
document.defaultView
CallWhat it does
document.defaultViewPrimary signature.

Built into the browser. Read or assign document.defaultView — no import required.

Library declarations
JavaScript
document.defaultView

Parameters

ParameterDescription
Writable?Optional. Read-only (MDN)
Null checkOptional. Some documents have no browsing context

Return Value

Return ValueDescription
*Window or null (MDN)

JavaScript Examples

Example 1 — defaultView vs window

On a normal page, the associated window is the global window.

JavaScript
console.log(document.defaultView === window); // true
console.log(document.defaultView.document === document); // true
Try It Yourself

How It Works

  1. Read defaultView — MDN: the window for this document.
  2. Compare with window — same object on the main page.
  3. Round-trip — defaultView.document points back to this document.
  4. Why it helps: libraries often pass a Document; this recovers its Window.

Example 2 — Viewport Size via defaultView

Use the window reference to read viewport dimensions.

JavaScript
const win = document.defaultView;
console.log(win.innerWidth);
console.log(win.innerHeight);
Try It Yourself

How It Works

  1. Get the window from the document.
  2. Read innerWidth / innerHeight — standard Window viewport APIs.
  3. Same as calling window.innerWidth on the main page.
  4. Pattern: Document → Window → browser chrome APIs.

Example 3 — Guard When defaultView May Be null

Programmatic documents may have no associated window — check before use.

JavaScript
const newDoc = document.implementation.createHTMLDocument("Temp");
const win = newDoc.defaultView;

if (!win) {
  console.log("no defaultView");
} else {
  console.log(win.innerWidth);
}
Try It Yourself

How It Works

  1. Create a free-standing document with createHTMLDocument.
  2. Read defaultView — often null (no browsing context).
  3. Guard with if (!win) before Window APIs.
  4. MDN reminder: return value may be null when no window is available.

Browser Support

Document.defaultView is a standard DOM property. MDN marks it Baseline Widely available (since July 2015).

✓ Baseline · Widely available

Document.defaultView

Returns the Window associated with a document, or null. Safe for everyday browser use.

99% Widely available
Google Chrome Yes
Yes
Microsoft Edge Yes
Yes
Mozilla Firefox Yes
Yes
Apple Safari Yes
Yes
Opera Yes
Yes
Internet Explorer Yes (legacy)
Yes
defaultView Excellent

Bottom line: Use freely in modern browsers. See the MDN defaultView reference.

Frequently Asked Questions

MDN: in browsers, the window object associated with a document, or null if none is available.
No. MDN marks Document.defaultView as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
On the main page document, usually yes — document.defaultView === window. Prefer defaultView when you only have a Document reference.
When no window / browsing context is available for that document — for example some programmatically created documents that are not displayed.
No. MDN: the property is read-only.
An iframe’s contentDocument.defaultView is the same Window as the iframe’s contentWindow when that document is accessible.

Next: designMode

Continue with the Document designMode property.

designMode →

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.

11 people found this page helpful