JavaScript - console.log()
Overview
Estimated time: 10–15 minutes
Use console utilities to inspect values, measure time, and format output.
Learning Objectives
- Print values and objects.
- Group logs and measure durations.
- Use tables and styling where available.
Basics
console.log('Value:', 42);
console.warn('Warning message');
console.error('Error message');
Objects and Tables
const user = { id: 1, name: 'Ada' };
console.log(user);
console.table([
{ id: 1, name: 'Ada' },
{ id: 2, name: 'Lin' }
]);
Groups and Timers
console.group('Load');
console.time('task');
// ...work...
console.timeEnd('task');
console.groupEnd();
String Substitution & Styling
console.log('x=%d y=%d', 2, 3);
console.log('%cStyled text', 'color: white; background: teal; padding: 2px 6px;');
Tips
- Avoid noisy logs in production.
- Prefer structured objects for easier inspection.
Checks for Understanding
- What’s the difference between
console.log
,console.warn
, andconsole.error
? - When might
console.table
be preferable to multipleconsole.log
calls?
Show answers
- They label output with different severities; warn/error may be highlighted and collected differently in consoles.
- When inspecting arrays of objects or object maps for a clearer tabular view.
Exercises
- Log an object and the same data using
console.table
; compare readability. - Wrap a series of logs inside
console.group
/console.groupEnd
with aconsole.time
/console.timeEnd
pair.