JavaScript console object
- console.assert(expression, message)
- console.clear()
- console.count()
- console.dir() and console.dirxml()
- console.error()
- console.group()
- console.info()
- console.log()
- console.table()
- console.trace()
- Conclusion
The console object gives you access to the browser’s console. It lets you output strings, arrays, and objects that help debug your code. The console is part of the window object, and is supplied by the Browser Object Model (BOM).
I used scirpt.js
files to presents some of behavior of console object:
Output:
console.assert(expression, message)
The purpose of the console.assert() function is ti test an expression is true or not. The console.assert() function is taking two parameters:
Assertion
- this is an boolean expression. If assertion is true nothing happens on the console. If assertion is false, the error is thrown on the console.ErrorMessage
- the error message which is shown in the error state on the console. (error styled)
Below is an example of error message that is thrown in console:
console.clear()
The purpose of this function is to clear the console. Burt why do you need to clear the console? Sometimes other libraries or frameworks can log many things on your console. For example, this code should display many logs in console:
Output:
The only thing that we can do it is to add:
to the end of our code. This should clear console from the every logs. To clear console editor we can use: CTRL + L
.
console.count(label)
Writes the number of times that count() has been invoked at the same line and with the same label.
Output:
After changing number of call of a function:
Output:
console.dir() and console.dirxml()
To show an HTML node as a JavaScript object instead of a XML representation we can use the console.dir() function. Basically console.dir(object)
is displaying JavaScript representation. This is the main purpose of this function.
console.dirxml(object)
this function is displaying the object in XML representation with all descendants. For example console.log()
and console.dirxml()
are identical. This two functions shows the same results.
console.error()
The console.error()
method writes an error message to the console. Example of use:
console.group()
Group functions are mainly making a group for other console functions with a title to create a block for debugging more clearly. This function allows to group objects into small blocks.
Output:
console.info()
console.info()
behave similar to console.log()
function but also shows an i
icon, an information icon next to the message and then shows the message.
console.log()
It’s one of the most commonly used JavaScript function all over the world. This function displays message to the console.
console.table()
This function displays arrays of object as a table.
Code:
console.trace()
This function displays a stack trace from the point where the method was called.
Output:
Conclusion
This is a deeper look into console object and some of the other methods. These methods are great tools to have available when we need to debug code.
There are a couple of methods that I have not talked about as their API is still changing. You can read more about them or the console in general here
and here
.
Reference:
My site is free of ads and trackers. Was this post helpful to you? Why not
Disqus is great for comments/feedback but I had no idea it came with these gaudy ads.