how to print in javascript – Code Example


In this article we will provide you code to print in javascript language. By printing you could mean two things –

  1. Printing the webpage in printer
  2. Displaying the content like printf in c language.

Code Example –

1. To print the webpage in printer or as PDF –


2. To print the content on console/terminal window –

a. Printing a table

console.table(["Tony", "Steve", "Bruce"])

output –

console.table example

Using an object –

console.table({"first name": ["Tony", "Steve", "Bruce"], "last name": ["Stark", "Rogers", "Banner"]})

Output –

displaying object in console.table

b. Printing debug logs –

console.debug("This is a debug log");

c. Printing error logs –

console.error("This is error log");

d. Printing info logs –"This is info log");

e. Printing warning logs –

console.warn("This is warn log");

f. Printing normal logs –

console.log("This is normal log");

g. Printing timer logs – Use it to get how much time an operation took –

console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff…");
console.timeEnd("answer time");