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 that 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 the uses of JavaScript console methods.
- LOG
- INFO
- WARN
- ERROR
- ASSERT
- COUNT and COUNTRESET
- TIME, TIMELOG and TIMEEND
- GROUP and GROUPEND
- TABLE
- CLEAR
In this article, we will discuss 10 useful JavaScript console methods that can help you to write more efficient and effective code.
1. Using `console.log()`
Method
In JavaScript, the console.log()
method is used to log or print messages to the console, which is a tool built into most web browsers and JavaScript environments. A console is a developer tool that allows you to see information about your code, such as debug information, error messages, and logged messages.
The console.log()
method takes one or more arguments and logs them to the console. For example, you can log a string like this:
1 | console.log("Hello, world!"); |
Or you can log a variable like this:
1 2 | let myNumber = 42; console.log(myNumber); |
You can also log multiple values at once by passing them as separate arguments:
1 | console.log("The answer is", myNumber); |
In addition to logging strings and variables, you can also use the console.log() method to log objects and arrays. For example:
1 2 3 4 5 | let myObject = {name: "John", age: 42}; console.log(myObject); let myArray = [1, 2, 3, 4, 5]; console.log(myArray); |
2. Using `console.info()`
Method
The console.info()
method in JavaScript is used to log an informational message to the console. It takes one or more parameters, which can be any data type, and outputs an informational message to the console with a blue “i” icon.
Here’s an example of using console.info():
1 2 | const username = 'JohnDoe'; console.info(`User ${username} is logged in.`); |
In the example above, the console.info()
method will output an informational message to the console indicating that the user with the username “JohnDoe” is logged in.
The console.info()
method is useful for logging non-critical information or updates about the state of the program. It can be used for debugging purposes or to provide context about the program’s execution.
3. Using `console.warn()`
Method
The console.warn()
method in JavaScript is used to log a warning message to the console. It takes one or more parameters, which can be any data type, and outputs a warning message to the console with a yellow warning icon.
Here’s an example of using console.warn()
:
1 2 3 4 | const age = 17; if (age < 18) { console.warn('User is under 18 years old. Proceed with caution.'); } |
In the example above, if the age variable is less than 18, the console.warn()
method will output a warning message to the console.
It’s important to note that console.warn()
is not the same as an error message. A warning message indicates that there is a potential issue that may not be critical to the operation of the code, whereas an error message indicates that there is a critical issue that must be fixed before the code can continue to run.
4. Using `console.error()`
Method
The console.error()
method in JavaScript is used to log an error message to the console with an error icon. It is used to indicate that an error or exception has occurred in the code, which may cause the application to fail.
Here’s an example of how to use the console.error()
method:
1 2 3 4 5 6 7 8 9 | const x = 5; const y = 0; try { const result = x / y; console.log(`Result: {result}`); } catch (error) { console.error(error); } |
In this example, we are attempting to divide the value of x by y, which is zero. This will result in a division-by-zero error, which we catch using a try-catch block. The console.error() method is then called, passing the error object as an argument. This will output an error message to the console with an error icon, along with a stack trace that shows the call stack at the time of the error. Using the console.error()
method is an effective way to debug and troubleshoot errors in JavaScript applications.
5. Using `console.assert()`
Method
Use console.assert()
method writes a message to the console based on a condition. It prints 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++){ console.count(); } |
By default it labeled the message with “default”, But you can change the label by passing it as an argument.
Use console.countreset() method to reset the counter, So you can use this again further.
1 2 3 4 | for (i = 1; i <= 5; i++){ console.count(); } console.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 the 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
The JavaScript console.clear() method is used to clear the console window of the browser’s developer tools. It erases all previously logged messages and any other output that was previously displayed in the console, making the console window blank and ready for new messages.
1 | console.clear(); |
This method is commonly used to clean up the console output when you want to focus on a specific set of messages or when you want to remove clutter from the console window. It can be called from the browser console or from JavaScript code running in the page.
Conclusion
In conclusion, JavaScript console methods are a powerful set of tools that can help you to debug and optimize your web applications. By using these methods, you can easily inspect and manipulate objects, measure performance, and log messages to the console. Whether you are a beginner or an experienced developer, these methods are essential for building robust and efficient applications. We hope that this article has provided you with a useful overview of these methods and has inspired you to explore them further in your own projects.
3 Comments
Cool stuff, thank you for that!
Some typos though, “console” is sometimes spelled “cosole” in the code sections.
Thanks Olivier, fixed the typos.
No, you have not fixed that typo that was reported the 9:th of June, 2022.
Section 8) What is “console.grouping()”?
Section 10) I suspect that “all the message” should read “all messages”.
Please explain the difference between the terms: “developer console”, “browser console”, “web console”, “debugging console” and just “console”.