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

  1. What’s the difference between console.log, console.warn, and console.error?
  2. When might console.table be preferable to multiple console.log calls?
Show answers
  1. They label output with different severities; warn/error may be highlighted and collected differently in consoles.
  2. When inspecting arrays of objects or object maps for a clearer tabular view.

Exercises

  1. Log an object and the same data using console.table; compare readability.
  2. Wrap a series of logs inside console.group/ console.groupEnd with a console.time/console.timeEnd pair.