How to print page using JavaScript ?

To print a page in JavaScript, use the print() method. It opens up the standard dialog box, through which you can easily set the printing options like which printer to select for printing. Page print in JavaScript is a simple code in JS used to print the content of the web pages.
  • The print() method prints the contents of the current window.(window.print)
  • It basically opens Print dialog box which lets you choose between various printing options.
Parameters: No parameters required Returns: This function do not return anything The following JS code shows a print button and then it shows the property of that webpage in a sheet in which you are going to print it. The JavaScript print function window.print() is used here to perform the functionality. Example :

<!DOCTYPE html>
           Print Page Using JavaScript

<script type="text/javascript">

    <h2>Welcome To  Codegyan</h2>

      <input type="button" value="Print" onclick="window.print()" />