Console

Tapaswi
4 min readJan 3, 2022

--

Oops! Something went wrong in code. Are you trying to debug using web browser console? Hurray! Then we do share similarities.

Almost all web browsers provide an interface known as ‘Console’ for developers to debug their code. Warnings, errors or general info is all we need as a developer to easily trace an error location in the sample of code.
For me, console is just not a debugger but a tool which I use very often to start with an algorithm, to try with some math operations or to play around with data objects stored in global variables.

In programming language, one could say that console is an object providing access to browsers debugging interface.

Although, we use console to print data object or some messages using console.log() very often, it is not restricted to that. The console consist of many methods which we don’t use generally but may be very useful to us.

console.log() : This method is well known and most common to us which can output anything of any type. Pass string, number, object or even html elements as a parameter and it will print same for you.

Print statements

console.clear() : If you wish to just clear of everything in console and start fresh then this is a method for you. Using this method, will drop you a message in console saying you just cleared console.

clean console

console.table() : This is an amazing method you can use for tidy visualisation of structured data or array in tabular format along with the raw data.

JSON Object
JSON array

console.count() : The method that counts the number of occurence of an event. It takes label as parameter and keeps track of that label occurence. If no label is specified then ‘default’ is used as default label.

count / countReset

console.countReset() : Counter can be reset using this method and similar to count it takes label as parameter and resets that particular instance.

console.trace() : I use this method mainly in catch block so that I get full info about an error; name of function, calling function, class and so on which helps me go and check at that particular location specified in trace.
console.error(): This method is similar to trace(), only difference is that it highlights error trace in red color, so that our focus is on the error.

trace / error

console.warn() : If you know that error is not that important then you can even convert it into warning where hightlight color is yellow.

warn

console.timeLog() : If you are interested in performance and want to check the time taken to complete the operation then timeLog is very much useful. To use timeLog, one has to call console.time() method which takes label as parameter and for each label you can define separate timer. Make use of timeLog() method to check time whereever you want to check time taken for execution but before calling timeEnd() method. Method timeEnd() will close the timer instance and you will not be able to log time further.

time/timeLog/timeEnd

console.assert() : Throwing error message based on condition is all what it does. First parameter will be condition that should pass and second parameter is error message that should be thrown on error. So it is obivious that you will see error message only in case of failure of condition.

assert

I hope you find this article useful. Happy Coding!

--

--