DEV Community

Dipankar Shaw
Dipankar Shaw

Posted on

5 useful console objects

Here is a list of 5 console objects other than console.log that can make your debugging less frustrating and make your console beautiful.

1. console.assert :

console.assert is just like a console.log with a built in condition checker just like if condition. But it slightly different then traditional if condition. A tradition if condition will run a block of code if the condition is true whereas console.assert will log anything if the condition is false. You can compare this with the else block.

Structure


console.assert(condition, output)
Enter fullscreen mode Exit fullscreen mode

Example

console.assert example

If you don't 0.1 + 0.2 = 0.30000000000000004 in javascript due to its computing techniques


2. console.error :

console.error is really helpful when you are using multiple console.log and few of them are for just logging any value and few of them are for show any kind of error. It has different styles then normal console.log so you can easily distinguish between them the value output and errors.

Structure

console.error("error text which you want to log")
Enter fullscreen mode Exit fullscreen mode

Example

console.error example


3. console.table :

console.table is very useful while working with any array or object. With this you can get a well structured table like view of your data which make it easy to read whereas console.log can make it a little bit messy.

Structure

console.table(object or array)
Enter fullscreen mode Exit fullscreen mode

Example

Array Output

logging array using console.table

Object Output

logging object using console.table

Nest Object Output

logging nested object using console.table

It could be more messy then console.log for nested objects otherwise it's a great option for none nested objects and arrays.


4. console.time :

console.time is very very important console object which everybody should know. It can log out the time taken for running one or more block of code which has no other option than manually measuring it using an stopwatch ⏱. To use this you have wrap the block of code with console.time() and console.timeEnd().
console.time() starts the timer then runs the block of code then console.timeEnd() stops the timer and log it in the console. It can have a argument as label for of the timer but it is not mandatory so you can skip it. But remember if you are using it both console.time() and console.timeEnd() must have same labels ohterwise it will through an error.

Its structure

console.time("test")
// some code which might take a time
console.timeEnd("test")
Enter fullscreen mode Exit fullscreen mode

Example

console.table example


5. console.warn :

console.warn is same as console.error just the difference is it decorates the log like as a warning ⚠ message and console.error decorate it as error message. You can use this to log any error for your fellow programmers or future self.

Structure

console.warn("some warning text")
Enter fullscreen mode Exit fullscreen mode

Example

console.warn example

Honorable Mention

console.info :

It is same as console.warn and console.error. But it is not that much decorated as those two. It only add a i icon before the log not much different then console.log.

console.info example

console.clear :

use this to clear the console.

Top comments (0)