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.
Before we talk about logging, think about your own experience writing code so far.
console.log() so much that your VS Code terminal output became messy and hard to read?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>
In VS Code, logs usually appear in two places:
When you run JavaScript like this:
node app.js
Any console.* output will print in the terminal.
When you run your code using the VS Code debugger, logs appear in the Debug Console panel.
console.log() The basic loggerconsole.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 readableLogging 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 sectionsIf 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 takesSometimes 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 outputIf 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
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>
Logging works best when your output is easy to understand
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:
10 the total or the discount?0.2 belong to?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.
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>
Too many logs make your terminal noisy and confusing. Log only what helps you understand what’s happening.
Logs are great while learning and debugging, but in real projects you remove the ones you don’t need anymore.