Introduction
If you are a web developer, then you must have used console.log()
many times. Although, I am not encouraging you to stop using console.log()
, you can still use it. But, there are other alternatives that can make your development more productive and fun.
console.info()
The console.log()
outputs a message to the console at the “info” log level. The message is shown with special formatting, such as a small “i” icon next to it.
Example:
console.dir()
The console.dir()
displays a list of the properties of the specified JavaScript object. In contrast to other logging methods, it doest prettify the object.
Example:
console.table()
The console.table()
displays the tabular data in table format. It takes one argument data, which should be an array or an object and logs the data in table format.
Example:
console.group()
The console.group()
creates a new inline group in the console. It indents the messages to an additional level, until the console.groupEnd()
is called. It helps to organize the output by visually associating related messages.
Example:
The output looks like this:
console.trace()
The console.trace()
outputs a stack trace to the console.
Example:
The output looks like this:
console.warn()
The console.warn()
outputs a warning message to the console. The message is shown with special formatting, such as yellow color and a warning icon next to it.
Example:
console.time()
The console.time()
starts a timer so that you can track how long an operation takes. You give each time a name. When you call console.timeEnd() with the same name, the browser will output the time elapsed (in milliseconds) since the timer was started. You can use console.timeLog()
to log the current value of the timer.
Example:
Conclusion
In this way, there are many other alternatives to console.log which you can use depending upon the nature and format you want.
P.S.: JavaScript offer various array functions or methods which makes handling of array very easy. You can read more here about 10 essential JavaScript function or methods in detail here.