- COUNT and COUNTRESET
- TIME, TIMELOG and TIMEEND
- GROUP and GROUPEND
console.log() method takes one or more arguments and logs them to the console. For example, you can log a string like this:
Or you can log a variable like this:
You can also log multiple values at once by passing them as separate arguments:
In addition to logging strings and variables, you can also use the console.log() method to log objects and arrays. For example:
Here’s an example of using console.info():
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.
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.
Here’s an example of using
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.
Here’s an example of how to use the
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 c
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.
6. COUNT and COUNTRESET
console.count() method is used to write a message on the console as the number of times it is called.
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.
7. TIME, TIMELOG and TIMEEND
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.
8. GROUP and GROUPEND
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.
console.table() method to print an object in form of table on browser console.