But do you know that Console API provides many more useful methods?
In this article, we'll explore different methods provided by the console object.
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).
Console Object Methods
Log a message and stack trace to console if the first argument is false.
In the above example, the expression
3 === 4 is false, so this method logs the same message which is passed as a second argument.
Clears the console.
Logs the number of times that this particular call to count() has been called.
You can pass a "label" as an argument.
Used to log error message to the console. Useful in the testing of code. By default, the error message will be highlighted with red color.
5. console.group() & console.groupEnd()
Creates a new inline group in the console. This indents following console messages by an additional level until
console.groupEnd() is called. They accept labels of the same value.
Outputs an informational message to the console.
Writes a table in the console view.
The first parameter is required and must be either an object or an array, containing data to fill the table.
8. console.time() & console.timeEnd()
Used to log warning message to the console. By default, the warning message will be highlighted with yellow color.
The console.trace() method displays a trace that shows how the code ended up at a certain point.
Custom Console Logs
Users can add Styling to the console logs in order to make logs Custom. The Syntax for it is to add the CSS styling as a parameter to the logs which will replace
%c in the logs.
Next time you want to debug your code use the above-listed methods. Thanks for reading. Please share it with your network. Don’t forget to leave your comments below.