console

A host API for writing diagnostic output — not part of the JavaScript language itself, but available in both browsers and Node.

                console.log(...values)
              

Purpose

console is a global object for writing diagnostic output. It isn’t defined by ECMAScript — it’s a host API that browsers and Node both happen to provide, with a compatible core set of methods, so console.log behaves the same in a browser DevTools console and a Node terminal.

Common methods

console.log('a plain message', someValue);
console.warn('something worth noticing, not fatal');
console.error('something went wrong');
console.table([
  { name: 'Ada', role: 'engineer' },
  { name: 'Grace', role: 'engineer' },
]);
  • console.log() — general-purpose output; accepts any number of values.
  • console.warn() / console.error() — same output mechanism, styled differently (and, in browsers, typically included in filtered error/warning views) so severity is visible at a glance.
  • console.table() — renders an array of similarly-shaped objects as a table, useful for scanning structured data quickly.

Behavior notes

  • console.log() itself returns undefined. Typing console.log('hi') directly into a REPL-style console therefore shows the printed line and then undefined for the expression’s own value.
  • Passing multiple arguments (console.log('x =', x)) logs them space-separated on one line, which is usually clearer than concatenating a string yourself.
  • Objects and arrays are shown as inspectable structures in a browser console, not just as flattened text — expandable in the DevTools UI.

Common mistakes

  • Treating console.log output as proof a value is a particular type — logging always renders a readable representation, which can look similar for genuinely different values (compare console.log("5") and console.log(5) — visually similar, but see Statements and Expressions’s sibling type material for why they aren’t the same).
  • Leaving debugging console.log calls in shipped code — fine during development, but a lint rule or review step should catch stray ones before merging.

Runtime notes

Available in both browsers and Node with a shared core method set; some methods (like console.table()’s exact rendering) differ cosmetically between hosts, and Node additionally writes console.error() output to stderr rather than stdout.

Taught in these lessons