When is the last time you use console.log method to debug your javascript code? Maybe just a minute ago, one hour ago or might one week ago. All of us here are very familiar with console.log but do you know that besides that method, we still have other great methods belong to console object to logging the message with different kinds of formatting? Let’s figure out now?

Take a quick look on console structure, we can see that console object give us many utility methods in order to help us debug, measure our javascript application easily:

console object
console object

In this post we will run thought each of them

console.assert

Syntax

console.assert(assertion, msg [, subst1, ..., substN]);

Parameters

assertionAny boolean expression. If the assertion is false, the message is written to the console.
msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usages

This utility helps us to write out the error messages only when under the assertion. The message is only printed out when the value of assertion is false, otherwise, nothing happens. We can gain the same effect with the combination of “if” statement and “console.error”. But console.assert is a more elegant way which could save us some typing:

console.error vs console.assert
console.error vs console.assert

console.assert
console.assert

This utility is extreme useful when we deal with a loop, we only print out the message in specific turns to avoid flooding the entire window with our error message.

console.clear

Syntax

console.clear()

Usages

Use to clean up the current console window when there are a lot of messages that are not used anymore, we can archive the same result with “ban-circle” ⃠ button, but with console.clear, we have a way to programmatically do it:

console.clear
console.clear
Clear console button
Clear console button

console.count & console.countReset

Syntax

console.count([label]);
console.countReset([label])

Parameters

labelIf supplied,  count()  outputs the number of times it has been called with that label. If omitted,  count()  behaves as though it was called with the “default” label.

Usages

console.count helps us to count how many time a block of code is executed, how many turns the loop runs or how many time the event is triggered. In addition, console.countReset helps us to start over the counting

console.count & console.countReset()
console.count & console.countReset()

console.info & console.debug & console.warn & console.error

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

Console.dir & console.dirxml

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

Console.group & console.groupCollaped & console.groupEnd

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

Console.dir & console.dirxml

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

console.memory

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

console.profile & console.profileEnd

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

console.table

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

console.time & console.timeEnd & console.timeLog & console.timeStamp

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

console.trace

Syntax

console.info(assertion, msg [, subst1, ..., substN]);
console.debug( msg [, subst1, ..., substN]);
console.warn( msg [, subst1, ..., substN]);
console.error( msg [, subst1, ..., substN]);

Parameters

msgA JavaScript string containing zero or more substitution strings.
subst1  …  substN JavaScript objects with which to replace substitution strings within  msg . This parameter gives you additional control over the format of the output.

Usage

These 4 methods are used when we want to print out the message on console window and don’t want the user sees it. They support different severity level for logging the message

Leave a Reply