Unleashing the Power of Console Mastery: Beyond console.log()

Ahmed salman
4 min readJan 21, 2024

--

JavaScript developers often rely on console.log for debugging purposes, but there are alternative methods and variations that can provide more insights into the code and make the debugging process more efficient. Let's explore some alternatives:

Debugging is an art every programmer must embrace. While we strive for bug-free code, reality often requires us to delve into the heart of our creations to uncover hidden errors. Your console is your trusted sidekick in this journey, and it offers far more secrets than the familiar console.log().

First Why would you use console.log?

console.log() is an important function in JavaScript that is used for printing messages to the console. It helps developers debug their code by displaying the output of their program in the console. Console.log is especially useful for troubleshooting problems and tracking the values of variables as the program runs. For more information, checkout the MDN Web Docs reference for console.log.

Ready to unlock the full potential of your console and streamline your debugging process? Let’s dive into these powerful techniques:

Prioritizing Messages with Precision:

  • Use console.warn() to highlight potential issues without interrupting code execution.
  • This is mostly used by NPM packages for angular/react framework to inform developers if the particular library or the API/version is deprecated or about any future changes to the library. Some web browsers add a small exclamation markin the web console for warnings indication.
console.warn("This is a warning");
  • Employ console.error() to flag critical errors that demand immediate attention like this example .

Tracking Performance Like a Pro:

  • Measure the execution time of specific code blocks with console.time() and console.timeEnd(), essential for optimizing resource-intensive tasks.
  • the console object has .time()and .timeEnd()methods that help with analyzing performance of pieces of your code. You first call console.time()by providing a string argument, then the code that you want to test, then call console.timeEnd()with the same string argument. You’ll then see the time it took to run the code in your browser console. Here’s an example:
console.time("Time this");
for (var i = 0; i < 10000; i++) {
// Your stuff here
}
console.timeEnd("Time this");

The code took about 0.4 milliseconds to run.

Most of the time code will execute too fast to be able to draw conclusions, so you’d most likely want to run your code in a for loop that goes through thousands of iterations.

Use this only in development mode and remove these calls in code that’s going into production.

Revealing Code’s Journey with console.trace():

  • Uncover the precise path a function took to reach its current state, pinpointing unexpected behavior and tangled execution flows.

Stack Trace

You can also do a stack trace with console. Just add it into a function.

function one() {
two()
}
function two() {
three()
}
function three() {
console.trace()
}
one()

In this example, we have very simple functions that just call each other. Then, in the last function, we call console.trace().

Organizing Messages for Clarity:

  • Group related console outputs using console.group() and console.groupEnd() to enhance readability and focus.
console.log("Test1!");
console.group("My message group");
console.log("Test2!");
console.log("Test2!");
console.log("Test2!");
console.groupEnd()

All the Test2 messages come under ‘My message group’.

A Fresh Start with console.clear():

  • Effortlessly wipe the console clean for a clutter-free debugging experience.

Visualizing Data Gracefully:

  • We can just pass array of objects in first parameter of console.table() method to see the array of objects as table.
const lstEmployees = [
{ id: 1, firstName: 'Roslyn', lastName: 'Rofson', age: 27 },
{ id: 2, firstName: 'Everett', lastName: 'Allen', age: 32 },
{ id: 3, firstName: 'Vildan', lastName: 'Ekici', age: 25 },
];
console.table(lstEmployees);

Output:

As shown in output, first column always shows index of the array when we pass array as data to console.table() method.

Adding a Touch of Style:

  • Inject a dash of visual flair with CSS within console messages using console.log("%c ...", "your-css-styles"), making key information pop!

Master these techniques, and you’ll transform your console from a humble logging tool into a versatile debugging command center. Embrace the power of clear communication, efficient performance analysis, and organized exploration to conquer any challenge your code presents. Happy debugging!

Thanks for reading the article so far. don’t forget too follow me and read the bast articles

Sources

Window Console Object (w3schools.com)

--

--

Ahmed salman
Ahmed salman

No responses yet