Skip to content
Explainer ยท Data Structures and Algorithms

How to Print in JavaScript: console.log, Printing Objects and Arrays, Page Output and window.print()

By Younus FardeenUpdated 4 Nov 20263 min read
Short answer

JavaScript has no print() function for the screen. To print values for debugging use console.log() (visible in the browser's DevTools or the Node.js terminal). To show text on a web page, set an element's textContent. To print the page itself on paper, call window.print(). In Node.js, process.stdout.write() prints without adding a new line.

"Print" means three different things in JavaScript: writing to the console, writing to the page, and sending the page to a printer. Pick the right one and you will avoid the usual confusion.

console.log() is the standard way to print while you build and debug. In a browser, open DevTools (F12) and look at the Console tab. In Node.js, the output appears in the terminal.

const name = "Asha";
const marks = [72, 85, 90];
console.log("Hello", name);
console.log(`Total marks: ${marks.reduce((a, b) => a + b, 0)}`);
console.log(marks);
console.log({ name, passed: true });

Output:

Hello Asha
Total marks: 247
[ 72, 85, 90 ]
{ name: 'Asha', passed: true }
  • Separate values with commas and console.log joins them with spaces.
  • Use a template literal, in backticks with ${...}, to put values inside text.

Other console methods

console.info("info: server started");
console.warn("warn: low memory");
console.error("error: file not found");
console.table([{ id: 1, name: "Asha" }, { id: 2, name: "Ravi" }]);
MethodUse
console.log()General output
console.info()Informational messages
console.warn()Warnings, shown highlighted
console.error()Errors, shown in red, and sent to stderr in Node.js
console.table()Arrays and objects as a table
console.group()Indent related lines
console.time() and console.timeEnd()Measure how long code takes

Format strings and pretty-print objects

console.log("%s scored %d (%f%%)", "Asha", 91, 91.5);
console.log(JSON.stringify({ a: 1, b: [2, 3] }, null, 2));

%s inserts a string, %d a number and %o an object. JSON.stringify(value, null, 2) turns an object into readable, indented text. Printing an object directly in the browser shows a live view, so it can look different from what it held when you printed it.

Show text on the web page

The console is for developers. Users need the text on the page itself:

<p id="result"></p>
<script>
  document.getElementById("result").textContent = "Marks: 91";   // show text on the page
  console.log("Also written to the console");                      // visible in DevTools
</script>
  • Use textContent for plain text. It is safe and fast.
  • Avoid innerHTML with user input, because it can run injected scripts.
  • Avoid document.write(). It can wipe the page if it runs after loading.
  • alert() is a pop-up box and blocks the page. Use it only for quick tests.
process.stdout.write("Loading");
for (let i = 0; i < 3; i++) process.stdout.write(".");
process.stdout.write("\n");

Output: Loading.... console.log always adds a new line, while process.stdout.write does not.

window.print() opens the browser's print dialog for the current page. It does not print a string. To control how the page looks on paper, add a @media print block in your CSS.

document.getElementById("print-button").addEventListener("click", () => window.print());

Common mistakes

  • Looking for print(): it does not exist in JavaScript for output, although window.print() opens the print dialog.
  • Leaving console.log calls in production code. They slow things down a little and can leak information.
  • Printing an object inside a string, which gives [object Object]. Use JSON.stringify or print the object separately.
  • Expecting a new line from process.stdout.write.

Next steps

Use the MDN reference for console, then practise with the software development path and the frontend project ideas.

Frequently asked questions

How do I print in JavaScript?

Use console.log() to print to the console, set an element's textContent to show text on a page, and use window.print() to send the page to a printer.

What is the difference between console.log and document.write?

console.log writes to the developer console, which users do not see. document.write inserts text into the page while it is loading and is discouraged in modern code.

How do I print an object in JavaScript?

console.log(object) in the console, or JSON.stringify(object, null, 2) to get readable text you can also show on a page.

How do I print on the same line in JavaScript?

In Node.js use process.stdout.write(). In a browser, build one string and log it once.

Is there a print function in JavaScript like Python's print?

No. console.log is the closest equivalent for debugging output.

Learn this with mentors

Programmes for builders and engineers with admissions open: live classes, mentors and project work.

Continue learning

Learning path

Data Structures and Algorithms learning path

Arrays to dynamic programming in order, with a problem plan.

Learning path

Software Development learning path

One language, DSA, full-stack, tests and deployment.

Explainer

Java syntax basics

Program structure, types, loops, methods and common errors.

Open →