Week 5 - Problem solving methods

Problem solving

Basic algorithms

Big O notation

Logging

Debugging

Unit testing

AI

Practice

Assignment

Back to core program

Logging

Logging is the practice of printing out useful information that helps you understand what your program is doing. It’s one of the simplest but most powerful tools for debugging, learning, and tracking your application’s behavior.

Whenever you're unsure what your code is doing — or why something is breaking — logs act like a flashlight showing you what's happening step by step.

Why logging matters

Before we talk about logging, think about your own experience writing code so far.

Questions for you

If any of these sound familiar, then you’re ready to learn how to use the console tools properly, not just console.log().

Logging helps you understand your program while it is running. It allows you to confirm that your code is executing, check what values variables contain, and track how data changes step by step. Logging is especially useful when you are learning JavaScript because it helps you build confidence about what your code is doing.

<aside>

💡 Without logging, you’re often programming in the dark.

</aside>

Where do logs appear in VS Code?

In VS Code, logs usually appear in two places:

1. Terminal

When you run JavaScript like this:

node app.js

Any console.* output will print in the terminal.

2. Debug Console

When you run your code using the VS Code debugger, logs appear in the Debug Console panel.

The most useful console methods

console.log() The basic logger

console.log() prints normal information.

You use it to inspect values and confirm what your program is doing.

const total = 10;
console.log("Total is:", total);

This is the most common form of logging and is great for everyday checks.

console.warn() Warnings (something looks wrong)

Sometimes your code still runs, but something unusual happens. That’s when you use a warning.

const discount = null;

if (!discount) {
  console.warn("⚠️ No discount provided, using default value");
}

Warnings help you spot issues early, even if they don’t crash your program.

console.error() Errors (something went wrong)

When something goes wrong and you want it to stand out clearly, use console.error().

const user = null;

if (!user) {
  console.error("❌ User not found");
}

Errors are useful for showing that something failed and needs attention.

console.table() Make arrays and objects readable

Logging arrays of objects with console.log() can be hard to read.

console.table() displays them in a clean table format.

const users = [
  { name: "Amina", age: 22 },
  { name: "Ravi", age: 25 },
];

console.table(users);

This is very helpful when working with API data.

console.group() Organize logs into sections

If your program prints many logs, it becomes hard to follow. Grouping helps.

console.group("User Details");
console.log("Name: Amina");
console.log("Age: 22");
console.groupEnd();

This makes your output look more structured and easy to scan.

<aside>

💡 Grouping logs makes your terminal output feel like a readable story.

</aside>

console.time() Measure how long something takes

Sometimes your code works, but you want to know whether it is slow.

console.time() helps you measure performance.

console.time("loop");

for (let i = 0; i < 1000000; i++) {}

console.timeEnd("loop");

The terminal will show how long that block took to run.

console.clear() Clear terminal output

If your terminal gets cluttered, you can clear it at the top of your program:

console.clear();
console.log("Starting fresh...");

This can be useful when you run the same program many times during development.

This video demonstrates the most useful console methods (like log, warn, error, table, and more) to make your output clearer and easier to understand. The speaker shows the logs in the browser console, but you can use the same console.* methods in your JavaScript files and see the output in the VS Code terminal when you run your code with Node.js.

https://www.youtube.com/watch?v=j7XwVztvKH4


Logging during debugging

When you are trying to understand why a function is behaving incorrectly, logging can help you follow what is happening while the program runs. Good logs can show you when a function starts, what input it receives, what happens inside loops, and what the final result is.

Example:

function calculateTotal(cart) {
console.log("calculateTotal called with:", cart);

let total =0;

for (const itemof cart) {
    total += item.price;
  }

console.log("Final total:", total);
return total;
}

This kind of logging is useful because it shows the program flow step by step, and helps you spot where things become unexpected.

<aside>

💡 Good logs should tell a story: what is happening, in what order, and with what values.

</aside>

Good logging habits

Logging works best when your output is easy to understand

Why logging without labels is a problem

When you log values like this:

console.log(total);
console.log(discount);

you will see output like:

10
0.2

At the moment you write the code, you might remember what those numbers mean. But a few minutes later or after adding more logs it becomes confusing.

You start asking questions like:

This becomes even worse when you have many logs inside loops or multiple functions. Your terminal output becomes a list of random values, and you spend more time trying to understand the output than actually solving the problem.

Labels make logs readable and useful

When you add labels:

console.log("total:", total);
console.log("discount:", discount);

your output becomes:

total: 10
discount: 0.2

Now you instantly know:

Labels also make your logs useful for someone else reading your code and even for future you when you come back later.

<aside>

</aside>

Don’t log too much

Too many logs make your terminal noisy and confusing. Log only what helps you understand what’s happening.

Remove logs you no longer need

Logs are great while learning and debugging, but in real projects you remove the ones you don’t need anymore.

What we should NOT log