Beyond console.log in JavaScript

devshetty

Deveesh Shetty

Posted on September 19, 2023

Beyond console.log in JavaScript

You may have only used console.log() in JavaScript, but you can do many more things with the console object.

console.table()

If you have an object and want a clear view of it instead of having those ugly braces in the console you can use console.table()

const obj = {
  name: "John",
  age: 30,
  city: "New York",
  country: "USA",
}
console.table(obj)
Enter fullscreen mode Exit fullscreen mode

console.table output

console.error()

You will get a cool-looking (yeah 🙂) red color message for the instances where you want to make the stress on the bug more realistic.

console.error("Why is this rendering twice")
Enter fullscreen mode Exit fullscreen mode

console.error output

console.warn()

Similar to error but a little less stressful, you can mostly use this as a warning to yourself.

console.warn("Fix this in the next release!")
Enter fullscreen mode Exit fullscreen mode

console.warn output

console.time()

You can get the computation time easily by using console.time(), which starts the timer, and console.timeEnd() which ends it to compute the time taken by your code.
You can pass a label inside the time() function which makes the timer unique, so you can have as many number of timers in your program.

console.time("Time")
let count = 0
for (let i = 0; i < 100000; i++) {
  count += 1
}
console.timeEnd("Time")
Enter fullscreen mode Exit fullscreen mode

console.time output

That's it for this blog... There are many more methods available, if you are interested you can check it out at MDN Docs.

Deveesh Shetty
Github | Twitter

đź’– đź’Ş đź™… đźš©
devshetty
Deveesh Shetty

Posted on September 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related