1. Introduction
The console object provides built-in methods for logging information, warnings, and errors, inspecting values, grouping related output, and verifying assumptions during development. Effective use of console methods, along with breakpoints in browser or Node.js debuggers, is one of the fastest ways to understand and fix bugs in JavaScript code.
Cricket analogy: Just as a team's video analyst uses replay footage and slow-motion breakdowns to pinpoint exactly where a batsman's technique breaks down, developers use console output and breakpoints to pinpoint exactly where their code's logic breaks down.
2. Syntax
console.log("info message");
console.info("info message");
console.warn("warning message");
console.error("error message");
console.assert(condition, "message shown only if condition is false");
console.group("label");
console.log("nested line");
console.groupEnd();
console.table(arrayOfObjects);
console.time("label"); /* ... */ console.timeEnd("label");3. Explanation
console.log, console.info, console.warn, and console.error all print output, but differ in severity and default styling — warn and error are often highlighted (yellow/red) and error also includes a stack trace in most environments. console.assert only prints its message when the given condition is falsy, making it useful for sanity-checking assumptions without cluttering output when everything is correct. console.group/groupEnd visually indent related log statements together, and console.table renders array/object data as a readable table.
Cricket analogy: console.log is like a routine ball-by-ball commentary note, console.warn is like the commentator flagging a risky shot selection in a highlighted tone, console.error is like the red 'WICKET' alert with full replay analysis, console.assert is like a stump mic that only beeps when a no-ball actually occurs, console.group nests all of one over's deliveries together in the scorecard, and console.table is like the full batting scorecard laid out in neat rows and columns.
Beyond the console, browsers and Node.js support setting breakpoints in DevTools or an IDE debugger, or inserting the debugger; statement directly in code to pause execution and step through logic line by line, inspecting variable values at each step.
Cricket analogy: Placing a debugger; statement in code is like a captain calling for a mandatory drinks break mid-over to freeze play and huddle the team to inspect exactly what's going wrong with the field placement before continuing ball by ball.
Gotcha: when you console.log an object and continue mutating it afterward, some environments show a live reference rather than a snapshot at log time — the console may display the object's later, mutated state instead of its state when logged, because objects are logged by reference. To capture a true snapshot, log a copy, e.g. console.log(JSON.parse(JSON.stringify(obj))) or console.log(structuredClone(obj)).
4. Example
console.log("Basic log message");
console.info("Info message");
console.warn("Warning message");
console.error("Error message");
let count = 0;
function increment() {
count++;
console.log(`Count is now ${count}`);
}
increment();
increment();
console.assert(count === 2, "Count should be 2");
console.assert(count === 5, "Count should be 5: assertion failed");
console.group("User Details");
console.log("Name: Alice");
console.log("Age: 28");
console.groupEnd();5. Output
Basic log message
Info message
Warning message
Error message
Count is now 1
Count is now 2
Assertion failed: Count should be 5: assertion failed
User Details
Name: Alice
Age: 286. Key Takeaways
- console.log/info/warn/error all print output but differ in severity, styling, and (for error) stack traces.
- console.assert only prints when its condition is false, prefixed with 'Assertion failed:'.
- console.group/groupEnd visually nest related log lines together for readability.
- console.table renders arrays of objects as a formatted table for easy scanning.
- Logged objects can appear as a live reference rather than a snapshot — clone before logging if you need the state at log time.
- The debugger; statement and browser/IDE breakpoints let you pause execution and step through code interactively.
Practice what you learned
1. When does console.assert(condition, message) print its message?
2. What is a key difference between console.error and console.log?
3. What do console.group and console.groupEnd do?
4. Why might console.log(someObject) show a mutated state instead of the object's values at the time of logging?
5. Besides console methods, what is another common way to pause and step through JavaScript execution for debugging?
Was this page helpful?
You May Also Like
Comments in JavaScript
Learn how to write single-line and multi-line comments in JavaScript to document and disable code.
Template Literals in JavaScript
Learn how backtick-delimited template literals enable string interpolation, multi-line strings, and tagged templates.
Error Handling in JavaScript
How JavaScript represents runtime failures with Error objects and the built-in TypeError, RangeError, ReferenceError, and SyntaxError types.
try-catch in JavaScript
The mechanics of the try, catch, and finally blocks, including execution order and how finally interacts with return statements.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics