How to Print in JavaScript

JavaScript provides a number of ways to print content from a web page. In this article, we’ll take a look at a few of the most popular methods and explore some of the pros and cons of each.

Checkout this video:

Printing basics

To print in JavaScript, you need to use the print() command. This will print the contents of the web page to the printer. You can also use the print preview feature to see how the web page will look when printed.

What is the print() method?

In JavaScript, the print() method is used to print the contents of a current window. It opens the Print dialog box, which lets you choose which printer to use and change any printing options. The print() method prints the contents of the current window.

How to use the print() method

In order to use the print() method, you will need to include a

Once you have done this, you can call the print() method from anywhere within your JavaScript code.

The print() method takes one parameter, which is the object to be printed. This can be a string, number, array, or any other data type. If no parameter is specified, nothing will be printed:

print('Hello world!'); //Prints 'Hello world!' to the console
print(123); //Prints 123 to the console
print([1, 2, 3]); //Prints [1, 2, 3] to the console

Printing options

JavaScript offers different printing options which allow you to control how content is printed. The print() method is used to print a page. The print() method opens the Print dialog box, which lets the user to select printing options.

How to print a specific element

In order to print a specific element, you will need to target that element with a unique identifier. For example, if you have a HTML element with the id of "Target":

This is the element I want to print!

You can target that specific element using the following JavaScript:

var Target=document.getElementById('Target');

How to print the whole page

To print the whole page, use the document.print() method:

Try it Yourself ยป
The document.write() method can also be used to write directly to a printer:

document.write("Hello world!");
print("Hello world!");

How to print without opening the print dialog

JavaScript provides the freedom to choose how to print. You can print without opening the print dialog by using the following code:


This code will open the print dialog automatically.

Browser support

Before you learn how to print in JavaScript, you need to be aware of the browser support. The window.print() method is supported in all major browsers, except OperaMini.

Which browsers support the print() method?

The print() method is supported by all major browsers.

Which browsers don't support the print() method?

While all major browsers support the print() method, there are some older versions of browsers that don't support this method.

The print() method is supported in all major browsers, including Internet Explorer, Firefox, Safari, and Chrome. However, some older versions of these browsers don't support this method.

If you need to support older versions of these browsers, you can use a polyfill such as the one provided by Print.js.

Printing tips

You can use the print function in JavaScript to print to the console or to a specific element on the page. You can also use the console.log function for debugging purposes. Let's take a look at some examples of how to print in JavaScript.

How to improve the print quality

There are a few things you can do to improve the print quality of your JavaScript projects.

1. Use a higher dpi for your images. A resolution of 300 dpi is ideal, but even 200 dpi will be an improvement over 72 dpi.

2. Use vector graphics whenever possible. Vector graphics are resolution-independent, so they will look sharp at any dpi.

3. When using raster images, make sure they are scaled appropriately. If an image is too small, it will be pixelated when printed; if it is too large, it will take up too much space on the page and may be cropped.

4. Use CSS to specify the page size and margins. This ensures that your content will be printed within the specified area and will not be cropped or truncated.

5. Use print-specific CSS rules to hide or show content as needed. For example, you can use the `@media print` media type to only include certain styles when the document is being printed.

By following these tips, you can ensure that your printed JavaScript projects look their best!

How to reduce the printing time

-First, check the settings on your printer. Many printers have settings that can help reduce printing time.

-Next, try printing in draft mode. This will print your document using fewer colors and lower-quality images, but it will generally print much faster than other modes.

-If you're printing a large document, consider breaking it up into smaller sections and printing those separately. This can help reduce the time spent sending data to the printer.

-Finally, make sure your computer is not doing anything else that could be slowing down the printing process. If you're scanning or copying a document at the same time you're trying to print, for example, this can greatly increase the amount of time it takes to print your document.


Before you print in JavaScript, you need to make sure that you have the right permissions. By default, JavaScript cannot print unless the user gives permission. This article will show you how to print in JavaScript with a few troubleshooting tips.

I can't print!

If you're having trouble printing, there are a few things you can check. First, make sure your printer is turned on and connected to your computer. Then, try restarting your browser. If that doesn't work, try clearing your browser's cache and cookies. If you're still having trouble, try using a different browser.

My printer prints blank pages!

If your printer is printing blank pages, there are a few things you can do to try to fix the issue.

First, make sure that your printer has paper in it and that the paper is not jammed. If the paper is jammed, remove it carefully following the printer's instructions.

Next, check your printer's ink or toner levels and replace or refill as necessary. Inkjet printers typically use ink cartridges, while laser printers use toner cartridges.

If your printer is still printing blank pages, there may be an issue with the printers settings. Try resetting the printer to its default settings and then print a test page. If that does not work, try updating the printer's drivers. You can usually find drivers on the manufacturer's website.

Scroll to Top