10 Console Methods in JavaScript

When we start learning JavaScript then at first learn about console.log(); to print the message on the browser. And we think that the JavaScript console has only one method. But that's not true because JavaScript contains a lot of console methods.

So to clear this confusion, in this post we are going to discuss the 10 Most Popular JavaScript console methods.


1). log

The console.log(); method is used to print a message on the web browser's console. This console method can print strings, numbers, arrays, and objects. Instead of displaying any message on the browser console, it logs messages to a debugging console.

console.log('Hello World Welcome to Geeks Help');


2). info

The console.info(); is used to display a message to the browser console. This console method is very similar to console.log(); but it is used by developers for permanent messages to output to the console in their code.

console.info("This is an information message");


3). warn

As the name suggests warn, this console method is used to print the warning message on the browser console. This console method takes a single argument of a message to be displayed on the browser's console.

console.warn("This is a warning");


4). Error

This JavaScript console method is used to print an error message in the console. It is mainly used for testing and troubleshooting purposes when we want to display any errors. This console method also takes a single argument of any type like String or Object.

console.error("Your application has a error");


5). Count

This console method is used to print a message on the console as the number of times it is called. Or It counts the number of every count and prints it on the browser console.

for (a = 1; a <= 5; a++) {
    console.count();
}



6). Assert

This javascript console method is used to print messages on the browser's console based on the conditions. It prints the message on the console when the assertion is failed. And if the assertion is true then nothing happens.

let a = 20;
let b = 3;
console.assert(b>a);


7). Time, Timelog and TimeEnd

The console.time(); method is used to start a timer to track the logs in the browser. Before starting the timer method the console.time(); method to start the timer and console.timeEnd(); to end the timer or to stop the timer.

And the console.timeLog(); is used to log the current value of the timer which was started from the previous console.time(); This is not used to start or end of the timer it is used only to print the current timestamp.

console.time("answer time"); //other block of codes

console.timeLog("answer time"); //other block of codes

console.timeEnd("answer time");


8). Group and GroupEnd

As this console method's name suggests that it is used to make a group of messages on the console of the web browser.

And the console.groupEnd(); the method is used to end a message group.

console.log("Mern Stack Developer RoadMap");

// First Group Started
console.group("Frontend Part");
console.log("Learn HTML");
console.log("Learn CSS");
console.log("Learn JavaScript");
console.log("Learn ReactJS");
console.groupEnd();
// First Group Ended

// Second Group started
console.group("Backend Part");
console.log("Learn NodeJS");
console.log("Learn ExpressJS");
console.log("Learn MongoDB");
console.groupEnd();
// Second Group Ended

// Printing a Warning
console.warn("Now It's Time to Build Some Projects");

// End Message
console.log("Thanks for visiting: geekshelp.in");




9). Table

This console method is used to print any object in the form of a table in the console of a web browser. To print the object in the form of a table you can use the code given below.

console.table({'name': 'raju webdev', 'role': 'Frontend Developer'});


10). Clear

As the name suggests clear. So, this console method is used to clear the messages from the console. It is good to use it at the staring of your code because it will clear all the previous logs but don't use it at the end of your all logs because then it will clear your all messages that you want to see.

console.clear();


🙋🏻‍♂️ Let me know in the comment section which console method was new for you in this post.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section