Tutorials JavaScript Output & Console
Ch 9 / 78
JavaScript Beginner Chapter 3

Output & Console

JavaScript has multiple ways to display output — from popup dialogs to the browser's developer console. Knowing which to use and when is the first step to effective debugging.

What you'll learn in this chapter
Use console.log() and related methods
Understand alert(), confirm() and prompt()
Output to the DOM using innerHTML
Open and use the browser DevTools console
Understand console.warn, error, table and group
Know which output method to use in each context

Five Ways JavaScript Can Output Data

JavaScript does not have a built-in "print" function. Instead it has several methods for sending data to different destinations — the console, the DOM, or a popup dialog.

console.log()
Developer Console
Prints output to the browser's DevTools console. The go-to tool for debugging — invisible to end users.
Use for debugging
alert()
Alert Dialog
Pops up a blocking modal dialog with a message and an OK button. Halts all JS execution until dismissed.
Avoid in production
innerHTML
DOM Output
Updates an HTML element's content directly. The correct way to display dynamic output to the user on the page.
Best for UI output
document.write()
Document Write
Writes HTML directly into the page during parsing. Overwrites the entire page if called after load — use only for quick tests.
Testing only
prompt()
Input Dialog
Shows a blocking dialog with a text input field and returns the user's input as a string (or null if cancelled).
Occasionally useful

console.log() — The Developer's Best Friend

console.log() is the most essential debugging tool in JavaScript. It writes messages to the browser's DevTools console without affecting the page. You can log strings, numbers, objects, arrays — anything.

JS — console.log basics
// Logging different data types
console.log("Hello, World!");          // string
console.log(42);                        // number
console.log(true);                      // boolean
console.log([1, 2, 3]);                 // array
console.log({ name: "Alex", age: 25 }); // object

// Multiple values in one call (comma-separated)
const name = "K2infocom";
const version = 2025;
console.log("Name:", name, "Version:", version);

// Template literals (ES6) — more readable
console.log(`Welcome to ${name}, version ${version}`);
Console Output
>"Hello, World!"
>42
>true
>▶ (3) [1, 2, 3]
>▶ {name: 'Alex', age: 25}
>Name: K2infocom Version: 2025
>Welcome to K2infocom, version 2025

All Console Methods

The console object has many methods beyond log. Each has a distinct purpose and visual style in DevTools:

Method Purpose Console Appearance
console.log() General output — any value Plain white/grey text
console.warn() Non-critical warnings Yellow background, ⚠ icon
console.error() Errors and exceptions Red background, ✖ icon
console.info() Informational messages Blue ℹ icon (some browsers)
console.table() Display arrays/objects in a table Interactive sortable table
console.group() Start a collapsible group Indented group with toggle
console.groupEnd() End the current group Closes the group
console.time() Start a performance timer Starts named timer
console.timeEnd() Stop timer and log elapsed ms e.g. "myTimer: 12.34 ms"
console.clear() Clear the console output Console is emptied
JS — all console methods in action
console.log("Regular log message");
console.warn("⚠ This is a warning!");
console.error("✖ Something went wrong");
console.info("ℹ Informational note");

// console.table() — great for arrays of objects
const users = [
  { name: "Alice", age: 30 },
  { name: "Bob",   age: 25 },
];
console.table(users);

// console.group() — group related logs
console.group("User Details");
console.log("Name: Alice");
console.log("Role: Admin");
console.groupEnd();

// Timing — measure performance
console.time("loop");
for (let i = 0; i < 1000000; i++) {}
console.timeEnd("loop"); // loop: 4.22 ms

Displaying Output in the Page — innerHTML

In real applications you rarely use alert() to show users data. Instead, you select an HTML element and update its innerHTML or textContent property.

JS — writing output to the DOM
// HTML: <p id="output"></p>

// innerHTML — can include HTML tags
document.getElementById("output").innerHTML =
    "<strong>Hello</strong>, World!";

// textContent — plain text only (safer, prevents XSS)
document.getElementById("output").textContent =
    "Hello, World!";

// Appending instead of replacing
document.getElementById("output").innerHTML +=
    "<br>New line added!";
innerHTML
Sets or gets the HTML markup inside an element. Can include tags. Use with caution — never insert untrusted user data here (XSS risk).
textContent
Sets or gets the plain text inside an element. HTML tags are treated as literal characters. Always prefer this for user-provided data.
innerText
Similar to textContent but is aware of CSS styling — won't return hidden text. Slightly slower. Prefer textContent in most cases.

Dialog Methods — alert, confirm, prompt

These three methods create native browser dialog boxes. They block all JavaScript execution until the user dismisses them — so use them sparingly, mainly for quick tests or critical confirmations.

JS — alert, confirm, prompt
// alert() — shows a message, waits for OK
alert("Welcome to K2infocom! 🎉");

// confirm() — returns true (OK) or false (Cancel)
const agreed = confirm("Do you accept the terms?");
console.log(agreed); // true or false

// prompt() — returns the user's input string, or null
const name = prompt("What is your name?", "Default");
if (name !== null) {
    console.log(`Hello, ${name}!`);
} else {
    console.log("User cancelled.");
}
Avoid in Production
alert(), confirm(), and prompt() block the entire browser tab. Many browsers now suppress them in iframes and some suppress them after repeated calls. Use custom modal dialogs in real applications.
⚡
Free Resource
Download our JavaScript Cheat Sheet
console methods, DOM APIs, ES6+ syntax — all on one page.
Get Sheet

Opening & Using Browser DevTools

The browser DevTools console is where all your console.log() output appears. It is also a fully interactive JavaScript REPL — type any JS and it runs instantly.

1
Open DevTools — press F12 or Ctrl + Shift + I (Windows/Linux) or Cmd + Option + I (Mac). Or right-click → "Inspect".
2
Click the "Console" tab at the top of the DevTools panel. This is where all console.log(), warn, and error messages appear.
3
Type JavaScript directly — click in the console input area (after the > prompt) and type any expression. Press Enter to run it. Try: 2 + 2 or document.title.
4
Filter by message type — use the filter buttons (Errors, Warnings, Info, Verbose) to reduce noise when debugging larger projects.
5
Clear the console — press Ctrl + L or call console.clear() from your script.

Try It Yourself

Edit the code below and click Run to see the output live. Try changing the console.log messages or the DOM output text.

Live Editor
✎ HTML / JS
👁 Preview
Quick Check — Chapter 3

Which method is the best practice for displaying output to users on a webpage?

Chapter Summary
  • console.log() is the primary debugging tool — outputs to DevTools, invisible to users.
  • Use console.warn() and console.error() for warnings and error messages with visual cues.
  • console.table() is perfect for visualising arrays and objects in a structured grid.
  • innerHTML / textContent is the correct way to display dynamic output on the page for users.
  • alert(), confirm(), prompt() block execution — use only for quick tests, never in production UI.
  • Open DevTools with F12 and use the Console tab to inspect logs and run JS interactively.

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel