JavaScript•Input/Output
JavaScript Console I/O: Logging, Tables, and Formatting
Learn how to inspect variables, print tabular data with console.table, and format runtime diagnostics.
Syntax
console.log("Output message", data);
console.table(arrayOfObjects);
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| console.log() | Standard Output | Prints formatted text and object representations. |
| console.table() | Tabular Output | Renders arrays/objects into clean visual tables. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing JavaScript locally:
// Advanced Console Output
const systemMetrics = [
{ service: "Auth API", latencyMs: 24, status: "OK" },
{ service: "Payment", latencyMs: 88, status: "OK" },
{ service: "Search", latencyMs: 142, status: "DEGRADED" }
];
console.log("--- System Diagnostics ---");
systemMetrics.forEach(m => {
console.log("[" + m.status + "] " + m.service + " -> " + m.latencyMs + "ms");
});Expected Output:
--- System Diagnostics --- [OK] Auth API -> 24ms [OK] Payment -> 88ms [DEGRADED] Search -> 142ms
How It Works: Step-by-Step
- Lines 2-6: Initializes metrics array of diagnostic objects.
- Lines 9-11: Iterates and prints structured status messages.
Key Concepts
- console.log accepts comma-separated arguments for inspection.
- In Node.js, process.stdout.write prints without adding an automatic trailing newline.
- console.error directs output to standard error (stderr).
Common Mistake: Logging objects with string concatenation [object Object]
Using "Data: " + obj calls toString(), resulting in "[object Object]". Use comma separation instead.
❌ Incorrect
const user = { name: "Alex" };
console.log("User: " + user); // "User: [object Object]"✅ Correct
const user = { name: "Alex" };
console.log("User:", user); // Prints full object structureRelated JavaScript Tutorials & Compiler
Node.js Stdin Input: Reading Multi-Line Input with fs.readFileSync(0)
Read single-line and multi-line standard input synchronously in Node.js 22 using fs.readFileSync(0, "utf8") before running your logic.
JavaScript Variables: let, const, and var Explained
Learn how to declare variables in modern JavaScript using let, const, and var with block scoping.
JavaScript Strings: Template Literals, Regex, and Methods
Explore string methods (split, replace, trim, includes) and template strings.
JavaScript Type Conversion and Coercion with Examples
Learn explicit vs implicit type conversion in JavaScript using Number(), String(), and Boolean().
JavaScript Online Compiler
Open in Compilra IDE
Run and test JavaScript on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.