JavaScript Window close() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.close() asks the browser to close the current window (or the Window you call it on) (MDN). It returns undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.close. Start with this line.

JavaScript
close() / window.close()
CallWhat it does
close() / window.close()Primary signature.
window.close(...)Call with the arguments your case needs.

Built into the browser. Call window.close(...) (or the global close(...)) — no import required.

Library declarations
JavaScript
close() / window.close()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Call

Call window.close() on the current window (often blocked on main tabs).

JavaScript
window.close();
// same as: close();
Try It Yourself

How It Works

  1. Call close. No arguments.
  2. Browser decides. Script-opened windows may shut.
  3. Main tab. Often ignored with a console warning.
  4. Return. Always undefined.

Example 2 — Opener Closes Popup

Open a popup, then close it from the page that opened it.

JavaScript
let popup = null;

function openPopup() {
  popup = window.open("", "DemoPopup", "width=320,height=200");
  popup.document.write("

Hello from the popup!

"); } function closePopup() { if (popup && !popup.closed) { popup.close(); } }
Try It Yourself

How It Works

  1. Open. window.open returns a reference.
  2. Store it. Keep popup for later.
  3. Close. Call popup.close() from the opener.
  4. Guard. Skip if null or already closed.

Example 3 — Self-Close Inside Popup

Put a Close button inside the popup that calls window.close().

JavaScript
const popup = window.open("", "SelfClose", "width=320,height=160");
popup.document.write(
  '

Thanks!

' );
Try It Yourself

How It Works

  1. Open a popup. Script-created, so it is closable.
  2. Write UI. Add a button that calls window.close().
  3. User clicks. The popup closes itself.
  4. Tip. Prefer user clicks over silent auto-close when possible.

Example 4 — Check .closed

After close(), read popup.closed to confirm status.

JavaScript
const popup = window.open("", "CheckClosed", "width=280,height=140");
popup.close();
console.log(popup.closed); // true
Try It Yourself

How It Works

  1. Open and close. Use a script-opened window.
  2. Read closed. Becomes true after shut.
  3. Why it matters. Avoid calling methods on a dead reference.
  4. Related. See Window.closed for the property itself.

Example 5 — Auto-Close After Delay

Close a popup after a short timer (still a script-opened window).

JavaScript
const popup = window.open("", "AutoClose", "width=300,height=160");
popup.document.write("

Closing in 2 seconds…

"); setTimeout(function () { if (popup && !popup.closed) { popup.close(); } }, 2000);
Try It Yourself

How It Works

  1. Open a popup. Show a short message.
  2. Start a timer. setTimeout waits 2 seconds.
  3. Close safely. Check the reference before close().
  4. UX tip. Tell the user why the window will close.

Browser Support

Window.close() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — not a Node.js built-in. Closability still depends on how the window was opened.

✓ Baseline · Widely available

close()

Close script-opened windows across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: API is universal; success depends on script-opened windows and user gesture / popup policy.

Frequently Asked Questions

It asks the browser to close the current window, or the Window object you call it on. It works reliably on windows your script opened (for example with window.open()).
No. Browsers block closing tabs the user opened via browser UI. close() is meant for script-opened windows. Otherwise you may see a console warning and nothing closes.
Keep the returned Window reference and call popup.close() from the opener, or call window.close() inside the popup itself.
No. Call close() with no arguments. The return value is undefined.
On a Window reference, closed becomes true after that window is shut. Check it before using the reference again.
No. MDN marks Window.close() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.

Next: confirm()

Ask OK / Cancel with window.confirm().

confirm() →

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.

8 people found this page helpful