100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Console and Debugging in JavaScript

Learn the core console methods and debugging techniques for inspecting and troubleshooting JavaScript code.

Basics & Data TypesBeginner8 min readJul 8, 2026
Analogies

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

javascript
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

javascript
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

text
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: 28

6. 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

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#ConsoleAndDebuggingInJavaScript#Console#Debugging#Syntax#Explanation#StudyNotes#SkillVeris