Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 156 Β· Getting Started

The Console and First Debugging Steps

Use console.log, table, warn, error, group, count and time to inspect your code, read JavaScript error messages with confidence, and check values with typeof.

Every programmer writes bugs. The difference between a frustrated beginner and a confident developer isn't fewer mistakes; it's knowing how to find them. Your first and most-used tool for that is the console.

You have already used console.log() to print values. In this lesson you will learn the rest of the console toolkit: logging several values with labels, showing data as tables, grouping output, counting and timing. Then you'll learn to read error messages calmly and follow a simple routine for tracking down problems.

console.log() with several values

console.log() accepts any number of values, separated by commas. It prints them on one line with spaces in between, and each value keeps its own type, so numbers stay numbers and objects are shown in full:

JS Logging several values

Press Run (or Ctrl + Enter) to see the console output here.

No output.

That last line is a favorite debugging shortcut. Writing { product, price } creates a small object whose property names match the variable names, so the output tells you exactly which value is which. You'll learn why this works in Objects.

Label your logs. When a program logs many values, a bare 12 in the console is a puzzle. console.log("cart total:", total) is instantly clear.

Levels: info, warn and error

The console has a few variations of log() that mark how important a message is. They work exactly like log() but are styled differently, and in browser DevTools you can filter the console to show only one level.

  • console.info() β€” general information (in most browsers, styled like log).
  • console.warn() β€” something looks wrong, but the program can continue. Shown in yellow.
  • console.error() β€” something has failed. Shown in red.
JavaScript
console.info("Loaded 24 products");
console.warn("Image for product 7 is missing, using a placeholder");
console.error("Could not save the order");

Using the right level makes real-world logs much easier to scan. Note that console.error() only prints a message; it doesn't stop your code the way a real error does.

console.table() for lists of data

When you have an array of similar objects, reading them one line at a time is slow. console.table() arranges them into rows and columns:

JS console.table

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The (index) column shows the array position or the object key. In browser DevTools, the table is interactive and you can sort it by clicking a column heading.

Grouping, counting and timing

A few more methods help when output gets busy:

  • console.group("label") starts an indented group; console.groupEnd() closes it. Groups can be nested.
  • console.count("label") prints how many times it has been called with that label. Great for checking how often something runs.
  • console.time("label") starts a timer and console.timeEnd("label") prints the elapsed time in milliseconds.
  • console.assert(condition, message) prints an error only if the condition is false.
JS group, count, time and assert

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The timing you see will differ from run to run and from computer to computer. That's normal: treat console.time() as a rough guide, not a precise benchmark.

Reading error messages

When JavaScript hits a problem it can't handle, it throws an error and stops running. The console shows a message like this one:

TEXT
Uncaught TypeError: Cannot read properties of undefined (reading 'length') (line 4)

Every error message has the same parts, and each one is a clue:

  1. Uncaught means nothing in your code handled the error, so the script stopped. (You'll learn to handle errors in Errors and try...catch.)
  2. The error type, such as TypeError, tells you the category of problem.
  3. The message describes what went wrong in plain-ish English.
  4. The location tells you where. In our editors it's the line number. Browser DevTools show the file name and line, plus a stack trace: the chain of function calls that led to the error. Click it to jump straight to the code.

The exact wording of messages differs between browsers, but the error types are the same everywhere. These four cover most beginner bugs:

Error type What it usually means Typical cause
SyntaxError The code breaks the grammar rules A missing bracket, quote or comma
ReferenceError A name doesn't exist A typo, wrong capitalization, or using a variable outside its scope
TypeError A value is the wrong kind for the operation Calling something that isn't a function, or reading a property of undefined or null
RangeError A value is outside the allowed range A negative array length, or a function calling itself forever

Run each of these lines on its own: uncomment one at a time and press Run.

JS Meet the common errors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the SyntaxError behaves differently: even the first console.log() doesn't run. A syntax error means the engine can't understand the code at all, so it refuses to start. All the other errors happen while the code runs, after the earlier lines have already done their work.

Checking types with typeof

Many bugs come from a value being a different type than you expected: text instead of a number, or undefined instead of an object. The typeof operator tells you what type a value is, as a string:

JS typeof as a debugging tool

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first line prints 34 instead of 7, and typeof reveals why: quantity is a string, so + joined the text instead of adding. Values read from form fields are always strings, so this exact bug is very common. You'll learn the full list of types and a few surprising typeof results in Data Types, and how to convert between them in Type Conversion and Coercion.

A simple debugging routine

When something doesn't work, resist the urge to change code at random. Follow these steps instead:

  1. Read the error message completely. Note the type, the message and the line number.
  2. Go to that line and read it slowly. For ReferenceError, check spelling and capitalization. For TypeError, ask which value on that line is not what you expected.
  3. Log the values involved just before the failing line: console.log({ user, items }). Check their types with typeof.
  4. Work backward. If a value is wrong, find where it was set and log there too.
  5. Change one thing at a time and run again, so you know which change fixed it.
  6. Clean up. Remove debugging logs once the bug is fixed.

If there's no error but the result is wrong, the same routine still works: log values at several points and find the first place where reality differs from your expectations. Later, in Debugging Techniques, you'll learn to pause code with breakpoints and step through it line by line.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Which method is best for viewing an array of objects as rows and columns?

    console.table() shows arrays and objects as a table. There is no console.list().
  2. 2. What does console.log({ total }) print when total is 12?

    The braces create an object whose property is named after the variable, so you see both the name and the value.
  3. 3. You get ReferenceError with the message that totl is not defined. What's the most likely cause?

    A ReferenceError means a name doesn't exist. The usual cause is a misspelled or wrongly capitalized name.
  4. 4. Your code has a missing closing quote on line 10. What happens to lines 1 to 9?

    Syntax errors are found while the engine reads the code, before it runs anything. Runtime errors like TypeError happen after earlier lines have run.
  5. 5. console.log("5" + 2) prints 52. Which check reveals the cause?

    typeof shows that "5" is a string, so + joins text instead of adding numbers.

Summary

  • console.log() takes any number of values. Log { name, value } objects to see names and values together.
  • info, warn and error mark the importance of a message; error() prints but doesn't stop your code.
  • console.table() shows lists of data as a table; group, count, time and assert help organize and measure.
  • Error messages have a type, a message and a location. SyntaxError stops the code before it starts; ReferenceError, TypeError and RangeError happen while it runs.
  • typeof reveals what type a value really is, which explains many bugs.
  • Debug methodically: read the error, inspect values, work backward, change one thing at a time.

Next: Data Types β€” the kinds of values JavaScript works with.