Web Analytics

Console & Debugging

Beginner ~9 min read

What is the Console?

The browser console is a powerful tool for viewing output, testing code, and debugging. It's your best friend when learning JavaScript!

Open the Console: Press F12 or Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac), then click the "Console" tab.

console.log() - Your Debugging Companion

console.log() prints messages to the console. Use it to check variable values, track code execution, and debug issues.

HTML
CSS
JS

Other Console Methods

The console has many useful methods beyond log(). Each serves a specific purpose!

HTML
CSS
JS

Console Method Reference

Method Purpose Example
log() General output console.log('Hi')
error() Error messages console.error('Oops!')
warn() Warnings console.warn('Careful')
table() Display as table console.table(data)
clear() Clear console console.clear()

Debugging with console.log()

Use console.log() strategically to track down bugs and understand code flow.

HTML
CSS
JS

Common Debugging Techniques

1. Check Variable Values

const result = calculateTotal();
console.log('Result:', result);

2. Track Function Execution

function processData() {
  console.log('processData started');
  // ... code ...
  console.log('processData finished');
}

3. Inspect Objects

const user = { name: 'Alice', age: 25 };
console.log('User object:', user);

4. Check Data Types

const value = '42';
console.log('Type:', typeof value);
Remember: Remove or comment out console.log() statements before deploying to production!

Summary

  • The console is essential for debugging JavaScript
  • console.log() displays values and messages
  • Use console.error() for errors, console.warn() for warnings
  • console.table() displays arrays/objects as tables
  • Strategic logging helps track code execution and find bugs
  • Open console with F12 or Ctrl+Shift+I

Quick Quiz

Which console method displays data in a table format?

A
console.log()
B
console.table()
C
console.grid()
D
console.display()