A JavaScript method is a property containing a function definition. A Console method is an object used to access the browser debugging console. With the help of console methods, we can print messages, warnings, and errors to the browser console, which is helpful for debugging purposes.
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.
- LOG
- INFO
- WARN
- ERROR
- ASSERT
- COUNT and COUNTRESET
- TIME, TIMELOG and TIMEEND
- GROUP and GROUPEND
- TABLE
- CLEAR
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.
1 2 | console.log("Hello World"); console.log("Msg", obj1, obj2); |
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.
1 | console.info("This is an information message"); |
3. WARN
Use console.warn() function to print warning message on browser console. It takes one argument of message to be displayed.
1 | console.warn("This is a warning"); |
You can also send an object to print on console message. For example:
1 2 | var myObj = { firstname : "Rahul", lastname : "K" }; console.warn(myObj); |
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.
1 | console.error("Your application has a error"); |
Alternatively use:
1 2 | var myObj = { firstname : "Rahul", lastname : "K" }; console.error(myObj); |
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.
1 | console.assert(document.getElementById("name"), "No element found with ID 'name'"); |
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.
1 2 3 | for (i = 1; i <= 5; i++){ cosole.count(); } |
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.
1 2 3 4 | for (i = 1; i <= 5; i++){ cosole.count(); } cosole.countreset(); |
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.
1 2 3 4 5 | console.time("answer time"); //other block of codes console.timeLog("answer time"); //other block of codes console.timeEnd("answer time"); |
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.
1 2 3 4 5 6 7 8 9 10 | console.log("This is the outer level"); console.group("First group"); console.log("In the first group"); console.group("Second group"); console.log("In the second group under first group"); console.warn("Still in the second group"); console.groupEnd(); console.log("Back to the first group"); console.groupEnd(); console.debug("Back to the outer level"); |
9. TABLE
Use console.table()
method to print an object in form of table on browser console.
1 | console.table({'id':1001, 'name':'tecadmin.net'}); |
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.
1 | console.clear(); |
Conclusion
Hope this tutorial helps you to understand the uses of JavaScript console methods.
2 Comments
Cool stuff, thank you for that!
Some typos though, “console” is sometimes spelled “cosole” in the code sections.
Thanks Olivier, fixed the typos.