In this article, You will learn about the JavaScript Console and types of console methods with examples of how to use them.
What is a console in JavaScript?
Console in JavaScript is an object that provides access to the browser's web console. It can be coupled with other approaches to achieve a variety of actions. Which is used for debugging and results logging.
- All modern browsers have a web console for debugging.
- Console is a global object, meaning it is accessible in the browser window's global scope. You can use it as a straight
console
orwindow.console
.
All current browsers provide a console that may be accessed through a keyboard shortcut key. To access the browser console:
- In Chrome and other chromium-based browsers, press the F12 key.
- For Mozilla Firefox, use the keyboard shortcuts CTRL + SHIFT + K.
- In Safari, use the keyboard shortcut keys Option + ⌘ + C.
It can also be accessed by right-clicking on a web page and choosing "Inspect" from the menu.
To open the console, click the Console option:
Console Methods
Console in JavaScript has several different methods. Among all the log methods is most commonly used.
1. console.log( )
This method is mainly used to log output to the console. We can use any type within log(), such as a string, array, or object.
Similarly, console.info
and console.error
can be used to display text on the console:
2. console.clear( )
It's used to clear the console.
3. console.time( ) & console.timeEnd( )
In order to display how many milliseconds a block of code or a function takes to run, use console.time()
and console.timeEnd()
:
4. console.table( )
It allows us to generate a table inside a console.
5. console.group( ) & console.groupEnd( )
It is used to move the output to the next level by indenting it:
6. console.count( )
It is used to count the number of times the function is called using this counting method.
7. custom console logs.
Users can add styling to the console logs in order to make logs custom.
Wrapping up
Many developers utilise the console to log data in order to see whether or not their code is functioning correctly. It enables the developer to read, edit, and modify JavaScript code while the browser is running.
JavaScript has a console object that can be used to log information to the browser's console.
If you enjoyed this article, then don't forget to give 💚 and If you have any suggestions or tips, feel free to leave a comment or contact me on Twitter at @ravinwashere. see you later!
Top comments (0)