Check If an Object Is Window in jQuery

This article explains how to determine whether an element or variable is a browser window object using jQuery's built-in $.isWindow() utility. You will learn the correct syntax, understand how the method works internally, review practical code examples, and discover the standard vanilla JavaScript alternative for modern development.

The $.isWindow() Method

jQuery provides the $.isWindow() utility function to test whether an argument is a window object. It evaluates the provided parameter and returns a boolean value: true if the object is a browser window, and false if it is not.

Syntax

$.isWindow(object);
  • object: The target object or element you want to test.

Practical Code Example

The following example demonstrates how $.isWindow() evaluates different types of inputs, including the global window object, standard DOM elements, and plain JavaScript objects:

// Testing the global window object
console.log($.isWindow(window)); // Output: true

// Testing a standard DOM element
const element = document.getElementById("my-div");
console.log($.isWindow(element)); // Output: false

// Testing a jQuery selection
console.log($.isWindow($(window))); // Output: false (it is a jQuery collection, not the raw window)
console.log($.isWindow($(window)[0])); // Output: true (the underlying DOM object is the window)

// Testing non-window types
console.log($.isWindow({})); // Output: false
console.log($.isWindow(null)); // Output: false
console.log($.isWindow(undefined)); // Output: false

How It Works Internally

Under the hood, jQuery checks whether the provided object is not null/undefined and whether it has a circular window property that points back to itself:

isWindow: function(obj) {
    return obj != null && obj === obj.window;
}

Because browser window objects contain a self-referential window property (window.window === window), this comparison confirms that the target is a browser context window without throwing reference errors on null or primitive values.

Deprecation and Vanilla JavaScript Alternative

jQuery.isWindow() was officially deprecated in jQuery 3.3.0 and is scheduled for removal in jQuery 4.0.0. To achieve the exact same functionality without relying on jQuery, you can define a native helper function using the same internal logic:

function isWindow(obj) {
    return obj != null && obj === obj.window;
}

// Usage
console.log(isWindow(window)); // true
console.log(isWindow(document)); // false