DEV Community

Cover image for Unravelling JavaScript Console API
Niharika Singh β›“
Niharika Singh β›“

Posted on

Unravelling JavaScript Console API

Nights that end with console.log() usually precede mornings that start with console.log(). πŸ˜”

Well, there's life beyond console.log().

Let's go over some JavaScript console methods that REALLY have the potential to make your life better.


0. console.log()

console.log("You already know this!");

// This is used to output a message to the console.

Output:

Alt Text

1. console.table()

Sometimes we want to output a large object on the console. Using console.log() can be a pain.

console.table() is a treat to sore eyes.

function Crypto(symbol, name) {
  this.symbol = symbol;
  this.name = name;
}

let bitcoin = new Crypto("$BTC", "Bitcoin");
let ethereum = new Crypto("$ETH", "Ethereum");
let polkadot = new Crypto("$DOT", "Polkadot");

console.table([bitcoin, ethereum, polkadot]);

Output:

Alt Text

How beautiful and incredibly readable does that look!

If you had used console.log() here, this is what you would have seen instead:

Alt Text

2. console.info() / console.error() / console.warn()

The console.info() method outputs an informational message.

The console.error() method outputs an error message.

The console.warn() method outputs a warning message.

console.info("FYI: Today is Monday");

console.error("Today is not Friday");

console.warn("Get back to work!");

Output:

Alt Text

πŸ’‘ It is a good practice to log messages on the console with appropriate formatting.

3. console.time()

This API method will tell you runtime in milliseconds. You can have multiple timers running at once in a JS program. (Maximum 10,000 timers.)

console.time("time taken");
//DO SOMETHING
let x = 90;
let y = 100;
let z = x + y / 100;
console.timeEnd("time taken");

Output:

time taken: 0ms - timer ended

These were some lesser known methods of console API in JavaScript.

I am hopeful that these methods will help you!

Cheers.

Top comments (10)

Collapse
 
benwinding profile image
Ben Winding

console.group() is pretty good too πŸ‘Œ

developer.mozilla.org/en-US/docs/W...

Collapse
 
niharrs profile image
Niharika Singh β›“

Yes, the output generated pretty interesting. When do you prefer using console.group() ?

Collapse
 
benwinding profile image
Ben Winding

It let's you nest console logs together, which can be handy for logging in certain components or services. I honestly don't use it much though

Collapse
 
sarthology profile image
Sarthak Sharma

console.table is my personal favourite to check arrays. Good article. πŸ‘πŸ»πŸ‘πŸ»

Collapse
 
niharrs profile image
Niharika Singh β›“

Thank you! Yeah, table() method is really handy.

Collapse
 
yeonlala profile image
yeonlala

Thanks! Good Post!

assert: Ζ’ assert()
clear: Ζ’ clear()
context: Ζ’ context()
count: Ζ’ count()
countReset: Ζ’ countReset()
debug: Ζ’ ()
dir: Ζ’ dir()
dirxml: Ζ’ dirxml()
error: Ζ’ ()
group: Ζ’ group()
groupCollapsed: Ζ’ groupCollapsed()
groupEnd: Ζ’ groupEnd()
info: Ζ’ ()
log: Ζ’ ()
memory: (...)
profile: Ζ’ profile()
profileEnd: Ζ’ profileEnd()
table: Ζ’ table()
time: Ζ’ time()
timeEnd: Ζ’ timeEnd()
timeLog: Ζ’ timeLog()
timeStamp: Ζ’ timeStamp()
trace: Ζ’ trace()
warn: Ζ’ ()
Collapse
 
abdisalan_js profile image
Abdisalan

Thanks for teaching me console.time! Great post!

Collapse
 
niharrs profile image
Niharika Singh β›“

You’re welcome! πŸ™πŸ»

Collapse
 
patrickweb profile image
Just Patrick

console.dir() has been a saver for me alot of times. Awesome post πŸ‘

Collapse
 
niharrs profile image
Niharika Singh β›“

That's great! Thank you.