The developer console in a web browser is a tool which logs the information associated with a web page, such as JavaScript, network requests, and security errors etc. To open the developer console window on Google Chrome, use the keyboard shortcut Ctrl + Shift + J (on Windows) or Ctrl + Option + J (on macOS). The console object provides multiple methods to use. In this tutorial, you will learn uses of JavaScript console methods. Here are basic details about all the above-listed functions and uses.
1. LOG
Use console.log() method to print a message on web console. It can print a string or any number of JavaScript objects. Instead of displaying anything on the browser console, it logs messages to a debugging console.
2. INFO
The console.info() function is used to display a message to the browser console. This is pretty similar to console.log but developers use it for permanent messages to output to the console in their code.
3. WARN
Use console.warn() function to print warning message on browser console. It takes one argument of message to be displayed.
You can also send an object to print on console message. For example:
4. ERROR
Use console.error() method to write an error message to the console. It is used for testing and troubleshooting purpose. It taken one argument of any type like String or Object.
Alternatively use:
5. ASSERT
Use console.assert() method writes a message to the console based on a condition. It print message to console only If the first argument expression evaluates to false.
6. COUNT and COUNTRESET
The console.count() method is used to write a message on the console as the number of times it is called.
By default it labled message with “default”, But you can change the label by passing it as an argument. Use cosole.countreset() method to reset the counter, So you can use this again further.
7. TIME, TIMELOG and TIMEEND
Use console.time() function to start a timer to track how long an operation takes. Before starting operating use time function to start timer and use the console.timeEnd() function once the operating finishes. With the help of this you can find the time taken by a specific operation by the application. You can also use console.timelog() to print the current time in your code. This will not start or end any timer, only print the current timestamp.
8. GROUP and GROUPEND
Use the console.group() function to make group of message on the browser console. This will start grouping of message next to it. Then use the console.grouping() function to end a group that started just before.
9. TABLE
Use console.table() method to print an object in form of table on browser console.
10. CLEAR
At the end use console.clear() to clear all the message from browser console. This is good to use at start, so it clears the console first before printing other messages.
Conclusion
Hope this tutorial helps you to understand the uses of JavaScript console methods.