What is a JavaScript console? Explained

What is a JavaScript console? Explained

ยท

3 min read

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 or window.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.

Chrome_inspect.png

To open the console, click the Console option:

chrome_console_option.png

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.

console_log_method.png

Similarly, console.info and console.error can be used to display text on the console:

console_error_info.png

2. console.clear( )

It's used to clear the console.

console_clear.png console_cleared.png

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():

console_time.png

4. console.table( )

It allows us to generate a table inside a console.

console_table.png

5. console.group( ) & console.groupEnd( )

It is used to move the output to the next level by indenting it:

console_group.png

6. console.count( )

It is used to count the number of times the function is called using this counting method.

console_count.png

7. custom console logs.

Users can add styling to the console logs in order to make logs custom.

custom_console.png

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!