JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 1: Discovering JavaScriptLesson 4 of 150About 40 minutes

Lesson 4: Using the console and understanding output

01Opening question

When a program gives the wrong answer, how can you see what it is thinking?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

Labels make output readable: each value is printed next to a word that says what it is.

const price = 25;
const quantity = 3;
console.log("price:", price, "quantity:", quantity);
console.log("line total:", price * quantity);

03Objective and prerequisites

Learning objective: Use console.log, console.warn, console.error, console.table and console.group to inspect values, and read output accurately (strings, numbers, arrays and objects).

You need: Lesson 3: Statements, expressions and comments

Skills practised: Console output, Debugging.

04Explanation

The console is your window into a running program. console.log can print several values at once: separate them with commas and they appear on one line with spaces between.

Add a label to every value you print, such as console.log("total:", total). Without labels, a column of numbers quickly becomes impossible to read.

console.warn and console.error print in yellow and red. Use them for messages that need attention. console.table shows a list of records as a table, and console.group indents related lines.

Strings and numbers can look identical when printed on their own (5 and "5"). Inside a list, strings are shown with quotes, so ["5", 5] reveals the difference.

05Definitions

console.log
Prints values to the console, separated by spaces.
console.warn / console.error
Print a warning or an error message, shown in a different colour.
console.table
Prints a list of records as a table of rows and columns.
Label
A short piece of text printed next to a value so you know what the value is.
Log level
The importance of a console message: log, info, warn or error.

Open the full glossary

06Visual learning object

Template literal builder. Build a labelled output line from values and see the exact text JavaScript produces.

Loading the interactive object…

Open the full Template literal builder page with its guided experiment.

07Examples

ex-004-01Several values in one log Browser-executable JavaScript

Commas separate values; console.log prints them with a space between.

const item = "Notebook";
const price = 4.5;
const quantity = 3;
console.log(item, price, quantity);
console.log("Subtotal:", price * quantity);
Sample input
None
Expected output
Notebook 4.5 3 Subtotal: 13.5
Common mistake
Printing values without labels, which makes long output hard to read.
Try this
Add a label to the first line so it reads: Item: Notebook Price: 4.5 Quantity: 3
Source
examples/ex-004-01-several-values.js

ex-004-02Log levels Browser-executable JavaScript

warn and error stand out from ordinary log lines. The academy console colours them, as browser tools do.

console.log("Checkout started");
console.warn("Only 2 items left in stock");
console.error("Payment failed: card declined");
console.info("Checkout will retry when the card is updated");
Sample input
None
Expected output
Checkout started Only 2 items left in stock Payment failed: card declined Checkout will retry when the card is updated
Common mistake
Using console.error for ordinary messages, which hides real problems among noise.
Try this
Add a console.warn for a coupon that expires tomorrow.
Source
examples/ex-004-02-log-levels.js

ex-004-03Tables for collections Browser-executable JavaScript

console.table lays out a list of records as rows and columns. console.log shows the same data on one line. You will learn about lists (arrays) and records (objects) in Level 2.

const cart = [
  { product: "Product A", price: 25, quantity: 2 },
  { product: "Product B", price: 40, quantity: 1 },
];
console.table(cart);
console.log(cart);
Sample input
None
Expected output
│ (index) │ product │ price │ quantity │ ├─────────┼─────────────┼───────┼──────────┤ │ 0 │ "Product A" │ 25 │ 2 │ │ 1 │ "Product B" │ 40 │ 1 │ [{product: "Product A", price: 25, quantity: 2}, {product: "Product B", price: 40, quantity: 1}]
Common mistake
Expecting console.table to sort or total the data. It only displays it.
Try this
Add Product C (price 15, quantity 3) to the list and run again.
Source
examples/ex-004-03-console-table.js

ex-004-04Trace a calculation with groups Browser-executable JavaScript

console.group indents the lines that follow until console.groupEnd, which keeps a step-by-step trace tidy.

const price = 25;
const quantity = 3;
const discount = 10;
console.group("Price check");
console.log("price:", price);
console.log("quantity:", quantity);
const subtotal = price * quantity;
console.log("subtotal:", subtotal);
console.log("after discount:", subtotal - discount);
console.groupEnd();
console.log("Done");
Sample input
price 25, quantity 3, discount 10
Expected output
Price check price: 25 quantity: 3 subtotal: 75 after discount: 65 Done
Common mistake
Forgetting console.groupEnd(), so every later line stays indented.
Try this
Change quantity to 4 and predict all four indented values before running.
Source
examples/ex-004-04-groups-trace-a-calculation.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Label your output

Make output self-explanatory with labels.

const width = 12;
const height = 7;
console.log(width * height);
console.log(width * 2 + height * 2);

Warn when stock is low

Use the right log level for each message.

const stock = 2;
console.log("Stock level:", stock);
// TODO: print the warning "Low stock: 2 left" with console.warn (use the stock value)
// TODO: print the error "Reorder required" with console.error

A debug log for a cart

Design readable diagnostic output for a calculation.

// Prices: Product A 25, Product B 40, Product C 15
// Quantities: A 2, B 1, C 3

09Prediction

Commit to an answer first, then run the real code to compare.

console.log("1" + 1);
console.log(1 + 1);
console.log("Total:", 1 + 1);
console.log(["1", 1]);

What are the four lines of output?

10Debugging investigation

The cart log shows [object Object]

A developer logs the cart to check its contents, but the console only shows Cart: [object Object], which hides the data needed to debug.

const cart = { items: 3, total: 135 };
console.log("Cart: " + cart);
console.log("Total:", cart.total);

More cases in the Debugging Detective Laboratory

11Practical scenario

Diagnosing a wrong cart total

When a cart total is wrong, professional developers do not guess. They log each intermediate value with a label (line totals, subtotal, discount, tax) and find the first value that is wrong. Stage 2 of the Shopping Cart project asks you to do this when one of its functions returns the wrong amount.

const subtotal = 90;
const discount = subtotal * 0.1;
console.log("subtotal:", subtotal, "discount:", discount);

12Knowledge check

1. What does this print?

console.log("Total:", 4 * 5);
  • Total: 4 * 5
  • Total:20
  • Total: 20
  • "Total:" 20

2. Which line shows a warning in the console's warning style?

  • console.log("warning: low")
  • console.warn("Low stock")
  • console.alert("Low stock")
  • warn("Low stock")

3. A log shows ["5", 5]. What does it tell you?

  • Both values are numbers
  • The first value is a string, the second a number
  • Both are strings
  • The list is broken

4. Why does this print Cart: [object Object]?

const cart = { items: 2 };
console.log("Cart: " + cart);
  • The object is empty
  • + converts the object to the text [object Object]
  • console.log cannot print objects
  • items must be a string

5. You want to see 20 product records as rows and columns. Which is the best choice?

  • console.error
  • console.table
  • console.group
  • console.count

6. What does console.groupEnd() do?

  • Deletes the grouped lines
  • Stops the indentation started by console.group
  • Ends the program
  • Prints the word end

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Label your output: Labelled area and perimeter; Still calculated from width and height
  • Warn when stock is low: Three lines at the right levels; Uses the stock value in the warning
  • A debug log for a cart: Prints the cart lines and total; Uses console.group and console.groupEnd; The total is calculated
  • ex-004-01: output matches the documented expected output
  • ex-004-02: output matches the documented expected output
  • ex-004-03: output matches the documented expected output
  • ex-004-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-01-discovering-javascript/lesson-004-using-the-console-and-understanding-output/. Every example on this page is generated from those files.

These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.

15Summary and next steps

  • Label every value you log.
  • Use warn and error for messages that need attention.
  • console.table shows records; console.group organises a trace.
  • Pass objects as separate arguments; joining them to strings with + hides their contents.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Debugging Plan my logging
Context: My shopping-cart total is wrong and I do not know which step fails.
Task: Suggest where to place labelled console.log lines to find the first wrong value.
Constraints: Do not fix the code; suggest at most five log lines with labels.
Expected behaviour: After running, I can point to the first incorrect value.
Testing: Ask me to paste the output and help me interpret it.
Concept explanation Log levels in real teams
Context: I know console.log, warn and error.
Task: Explain how professional teams use log levels in production applications.
Constraints: Under 200 words, no specific vendor products.
Expected behaviour: I can decide which level fits a message.
Testing: Give me five messages to classify.
Code review Review my debug output
Context: I wrote a 15-line debug log for a calculation.
Task: Review whether the labels and grouping make it easy to read.
Constraints: Suggest improvements without changing the calculation.
Expected behaviour: The output reads as a clear trace.
Testing: Show the improved output, not only the code.
Code prediction Strings or numbers?
Context: I am learning to read console output.
Task: Give me six console.log lines mixing strings, numbers and small lists, and ask me what each prints.
Constraints: Wait for my answers before revealing.
Expected behaviour: I can tell a string from a number in a list.
Testing: Explain each answer in one sentence.
Testing Check log levels
Context: An exercise checks both the text and the level of my console messages.
Task: Explain why a test might check the log level as well as the text.
Constraints: Two short paragraphs.
Expected behaviour: I understand why level matters for monitoring.
Testing: Give one example of a production problem caused by the wrong level.