DEV Community

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

Posted on

40 9

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs