Output in JavaScript
Introduction
Programs need to show results—to learners, users, or operators debugging production issues. In Node.js and browsers, console is the first output tool you will use every day. This chapter covers practical console methods, formatted messages, and how output differs between terminal and DevTools.
Prerequisites
- JavaScript syntax basics
- Ability to run
node your-file.jsfrom VS Code terminal or system shell
The console Object
console is a built-in object with methods for logging. In Node.js and browsers, it is available without importing anything in simple scripts.
// Basic text output to the terminal (Node) or DevTools console (browser)
console.log("Hello from JavaScript");console.log — Everyday Messages
Use log for general information during learning and debugging.
// Log a string and a number on separate lines
console.log("Order status: shipped");
console.log("Tracking id:", 88421);console.log accepts multiple arguments and prints them separated by spaces:
// Multiple values in one call
const user = "Sam";
const role = "admin";
console.log("User:", user, "Role:", role);Template Literals for Readable Output
Combine console.log with template literals for interpolation:
// Embed variables inside backtick strings
const product = "Keyboard";
const price = 49.5;
console.log(`Added ${product} at $${price}`);Other Useful console Methods
| Method | Typical use |
|---|---|
console.error | Errors and failures |
console.warn | Warnings, deprecations |
console.info | Informational notes (similar to log in many environments) |
console.table | Display arrays/objects as tables |
// Different severity channels
const port = 3000;
console.info(`Server listening on port ${port}`);
console.warn("Cache is almost full");
console.error("Database connection failed");
// Table view for structured data
const users = [
{ name: "Ada", score: 98 },
{ name: "Lin", score: 87 },
];
console.table(users);Tip
Best Practice
Use console.error for real error paths so log aggregators can filter severity in production later—even while learning.
Output in Node.js vs the Browser
| Environment | Where output appears |
|---|---|
| Node.js terminal | Integrated terminal / shell stdout |
| Browser | DevTools Console tab |
The API is largely the same for console.log, which is why tutorials often start there.
// Same call works in Node and browser consoles
console.log("Environment:", typeof window === "undefined" ? "Node.js" : "browser");Formatting with Placeholders (Optional)
console.log supports format-style placeholders in some engines:
// %s = string, %d = number (behavior may vary slightly by environment)
const name = "Mia";
const count = 3;
console.log("User %s placed %d items", name, count);Template literals are usually clearer for new projects—use whichever your team standardizes.
Mini Example: Order Summary
Save as output-demo.js:
// Build a tiny order summary for the console
const item = "Notebook";
const quantity = 2;
const unitPrice = 6.5;
const total = quantity * unitPrice;
console.log("--- Order summary ---");
console.log(`Item: ${item}`);
console.log(`Qty: ${quantity} x $${unitPrice}`);
console.log(`Total: $${total.toFixed(2)}`);
console.log("---------------------");Run:
node output-demo.jsWhat console Is Not
- Not a UI for end users (websites use HTML/CSS for visible pages)
- Not a replacement for file logging or monitoring systems at scale
- Not always captured in production the same way—teams later adopt structured loggers
For learner workflows, console.log remains the fastest feedback loop.
FAQ
What is the difference between console.log and console.info?
In Node.js they often look similar. Some tools style them differently. Follow team conventions; both are for non-error messages.
Can I print without a newline?
console.log adds a newline by default. process.stdout.write in Node can print without one—advanced topic; log is enough for now.
Why do I see undefined printed sometimes?
console.log returns undefined, and the REPL may display return values. In scripts inside .js files you usually only see what you explicitly log.
How do I clear the console?
Browsers: console.clear(). Terminals: use the terminal’s clear command or restart output—behavior varies.
What chapter comes next?
Statements explains how instructions combine into programs, then Comments.