Using of Console in java script

Piyush AgrawalPiyush Agrawal
2 min read

The console object in JavaScript provides a set of functions that allow you to interact with the browser's developer console. These functions are useful for debugging, logging information, and monitoring the execution of your JavaScript code. Here are some of the commonly used console functions:

  1. console.info()

    • Outputs an informational message to the console.
console.info("This is an informational message.");
  1. console.info()

    • Outputs an informational message to the console.
console.info("This is an informational message.");

3.console.warn()

  • Outputs a warning message to the console
console.warn("This is a warning message.");

4.console.error()

  • Outputs an error message to the console.
console.error("This is an error message.");

5.console.table()

  • Displays tabular data as a table.
const data = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
];
console.table(data);
  1. console.group() / console.groupEnd()
  • Groups log messages together.
console.group("Group 1");
console.log("Message 1");
console.log("Message 2");
console.groupEnd();

7.console.count()

  • Outputs the number of times that console.count() has been called.
for (let i = 0; i < 3; i++) {
  console.count("Loop iteration");
}

8.console.time() / console.timeEnd()

    • Measures the time it takes to execute a block of code.
console.time("Timer");
// Code to be timed
console.timeEnd("Timer");

9.console.trace()

  • Outputs a stack trace to the console.
function exampleFunction() {
  console.trace("Trace example");
}

exampleFunction();

10.console.clear()

  • Clears the console.
console.clear();

These functions are just a subset of what the console object provides. Depending on your needs, you can choose the appropriate function to log information at different levels and formats.

note: some features like (console.table) and(console.group)might not be available in all browsers or environments.

0
Subscribe to my newsletter

Read articles from Piyush Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Piyush Agrawal
Piyush Agrawal