JavaScript Window print() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.print() opens the print dialog for the current document (MDN). No parameters. Returns undefined.

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

Syntax

Call window.print. Start with this line.

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

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

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

Parameters

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

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Call

Open the print dialog from a button.

JavaScript
// inside a click handler
window.print();
// same as: print();
Try It Yourself

How It Works

  1. Call print. No arguments.
  2. Dialog. Browser shows print / Save as PDF UI.
  3. User chooses. Print, cancel, or save.
  4. Return. Always undefined.

Example 2 — Confirm First

Ask before opening the dialog — good UX for accidental clicks.

JavaScript
if (confirm("Print this page?")) {
  window.print();
} else {
  console.log("cancelled");
}
Try It Yourself

How It Works

  1. Confirm. confirm() returns true / false.
  2. Branch. Only call print() on OK.
  3. Cancel path. Skip the dialog if the user declines.
  4. Tip. Custom UI is nicer than nested system dialogs in production.

Example 3 — Print a Popup

Open a small window with only the content you want, then print it.

JavaScript
const popup = window.open("", "PrintWin", "width=480,height=360");
if (!popup) {
  console.log("blocked");
} else {
  popup.document.write("

Invoice

Total: $42

"); popup.document.close(); popup.focus(); popup.print(); }
Try It Yourself

How It Works

  1. Open. Create a blank named window (allow popups).
  2. Write content. Only what should appear on paper.
  3. print on popup. popup.print() prints that document.
  4. Alternative. Hide nav with @media print on the main page instead.

Example 4 — beforeprint / afterprint

Run code when the dialog opens and when it closes.

JavaScript
window.addEventListener("beforeprint", function () {
  console.log("before");
});
window.addEventListener("afterprint", function () {
  console.log("after");
});

window.print();
Try It Yourself

How It Works

  1. beforeprint. Fires as print/preview starts.
  2. afterprint. Fires when printing ends or the dialog closes.
  3. Use case. Temporary layout tweaks that CSS cannot cover.
  4. Prefer CSS. MDN: use @media print for most hide/show needs.

Example 5 — Return Value

Confirm that print() always returns undefined.

JavaScript
const result = window.print();
console.log(result); // undefined
Try It Yourself

How It Works

  1. Call print. Dialog opens (blocking).
  2. Dialog closes. Assignment completes.
  3. Value. Always undefined — no success/fail flag.
  4. Tip. You cannot detect “user cancelled” from the return value.

Browser Support

Window.print() is marked Baseline Widely available on MDN (since June 2023). Browser Window API.

✓ Baseline · Widely available

print()

Open the print dialog 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: Safe to use for Print buttons. Pair with @media print for clean output.

Frequently Asked Questions

It opens the browser print dialog for the current document so the user can print or save as PDF. JavaScript cannot skip the dialog or force a silent print.
No. MDN marks Window.print() as Baseline Widely available (since June 2023). It is not Deprecated, Experimental, or Non-standard.
No. Call print() with no parameters. The return value is undefined.
Yes. MDN: the method blocks while the print dialog is open. Code after print() runs only after the dialog closes.
beforeprint fires when the document is about to be printed or previewed; afterprint fires when printing finishes or the dialog closes. Prefer @media print CSS when you only need layout changes.
Not with a parameter. Hide sections with @media print, or open a popup/iframe with only the content and call print() on that window.

Next: prompt()

Ask the user for a line of text with a modal dialog.

prompt() →

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.

9 people found this page helpful