Javascript console methods and functions

Javascript provides different console methods and functions for interacting with the browser's developer console. These methods are useful for debugging and logging information from your JavaScript code.

Javascript image

Here are some commonly used console methods and functions.

1. console.log() :

This method is used to print a message or value to the console. Consider following syntax/example for better understanding which prints simple message to console.

This is generally used when you want to print any code result into console.
console.log("This is a cat!");

2. console.info() :

This is used to logs an informational message to the console. It's similar to console.log() but used to provide additional context.

Consider following syntax/example for better understanding where a message/information is printed in console using this method.
console.info("Informational message!");

3. console.clear() :

Used to clear the console, it removes all messages and logs from console. Consider following syntax/example for better understanding.

You can use this method to clear your console window, this is specially useful when you have too many information already printed in console and you need to clean console window.
console.clear();

4. console.table() :

It used to display data in a tabular format in console, making it easier to visualize arrays and objects in console.

Consider following syntax/example for better understanding. As per following example we have one array of object named people which contains 2 objects. When you print this object in console using this method it will display result in table format so you can easily visualized your data same as your excel sheet/google sheet.

To test this amazing feature just copy paste following code into your code editor and hit run button.
const people = [{ name: "John", age: 30 }, { name: Alice", age: 25 }];
console.table(people);

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

This is used to group console logs together to organize output. Useful for structuring complex logs. Consider following syntax/example for better understanding.

It's specially useful when you have too many results in your console and you want to categorize those results. When you use this method to group console results than it will create one group which can be collapsed and or expanded directly in console. 

To test this amazing feature just copy paste following code into your code editor and hit run button.
console.group("Group 1");
console.log("Log 1");
console.log("Log 2");
console.groupEnd();

6. console.assert() :

Tests if a condition is true and, if not, logs an error message. It's used for debugging assertions. Consider following syntax/example for better understanding.
const x = 10;
console.assert(x === 5, "x is not 5");

7. console.time() & console.timeEnd() :

This is used to measure the time it takes to execute a piece of code. Useful for structuring complex logs. Consider following syntax/example for better understanding.

It's useful when you want to measure execution time of your code and using this you can check your code efficiency and calculate time of execution.

Want to try this amazing feature? Just paste this syntax into your code editor and hit run button.
console.time("Time");
// Write your code here
console.timeEnd("Time");

8. console.count() :

This is useful for tracking how many times a specific part of your code runs. Consider following syntax/example for better understanding.

It's specially useful when you want to count something and print in console.
for (let i = 0; i < 5; i++) {
  console.count("Loop Iteration");
}
Thanks for reading this article, if you found this article useful then share it with your friends and share your thoughts in comment section.

Want to learn more about javascript? checkout these amazing  Javascript Tutorials .

Comments