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.
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() — 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.
// 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}`);
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 |
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.
// 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!";
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.
// 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.");
}
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.
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.
console.log(), warn, and error
messages appear.> prompt) and type any expression.
Press Enter to run it. Try: 2 + 2 or document.title.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.
Which method is the best practice for displaying output to users on a webpage?
- 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
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.