The Console
console.log, errors, warnings, and running JavaScript live
Skills taught: web.console-api, web.reading-errors, web.js-repl
The console API
// Basic output
console.log('Hello, world!'); // white text
console.info('Page loaded'); // same as log
console.warn('Deprecated API used'); // yellow, warning icon
console.error('Fetch failed: 404'); // red, error icon, stack trace
// Structured output
console.table([ // renders as a table
{ name: 'Alice', score: 95 },
{ name: 'Bob', score: 80 },
]);
console.dir(document.body); // explore an object's properties
// Grouping
console.group('User data'); // collapsible group
console.log('Name: Alice');
console.log('Role: admin');
console.groupEnd();
// Timing
console.time('fetch-users'); // start timer
await fetchUsers();
console.timeEnd('fetch-users'); // prints: 'fetch-users: 84ms'
// Assertions
console.assert(user.age > 0, 'Age must be positive'); // only logs if false
// Counting
console.count('click'); // click: 1
console.count('click'); // click: 2
Reading error messages
When JavaScript throws an error, the Console shows three key pieces of information: the error type, the error message, and a stack trace showing exactly which line caused the problem.
Uncaught TypeError: Cannot read properties of undefined (reading 'name')
at getUserName (app.js:42:18) ← line 42, column 18 in app.js
at renderHeader (app.js:15:5) ← called from renderHeader
at app.js:3:1 ← called from top of file
┌─ Error type: TypeError
├─ Message: Cannot read properties of undefined (reading 'name')
└─ Translation: You tried to access .name on something that is undefined
→ the variable you used is undefined, not an object
Fix: Check that the variable is not null/undefined before accessing .name
const name = user?.name ?? 'Unknown';
- TypeError — wrong type (accessed property on undefined/null, called non-function)
- ReferenceError — variable does not exist (typo, wrong scope, forgot to import)
- SyntaxError — invalid JavaScript syntax (missing bracket, unexpected token)
- RangeError — value out of valid range (array length negative, too many recursion levels)
Tip: Click the file:line link in a stack trace (e.g. app.js:42) to jump directly to that line in the Sources panel with the code highlighted.
The Console as a JavaScript REPL
You can type any JavaScript expression into the Console and press Enter to run it. This gives you live access to the page's JavaScript context.
// Inspect the DOM
document.title // → 'KERNAL — The core of every coder'
document.querySelector('h1').textContent // → 'The Three Layers of the Web'
document.querySelectorAll('p').length // → 14
// Manipulate the page live
document.body.style.background = 'red' // page background turns red
document.title = 'Hacked!' // changes the tab title
// Inspect stored data
localStorage.getItem('kernal_progress') // see your saved progress
JSON.parse(localStorage.getItem('kernal_progress'))
// DevTools shortcuts
$0 // the element currently selected in Elements panel
$('h1') // shorthand for document.querySelector('h1')
$('p') // shorthand for document.querySelectorAll('p')
$_ // the result of the last expression
Frequently Asked Questions
Should I remove console.log before deploying?
Yes, in production. console.log statements slow down your app slightly and expose debugging information that end users and malicious actors can see. Use a build tool (Vite, webpack) with a terser plugin that strips console statements automatically in production builds. Keep them during development — they are your best debugging tool.
What is a stack trace?
A stack trace is the list of function calls that led to the error — most recent at the top. Reading it bottom-up shows the chain: what started the chain (the entry point) → through what intermediate functions → to the line that crashed. The first line mentioning your own code (not a library) is usually where the bug is.