Explain all Console Object in HTML


A console is an object in the JavaScript programming language that is used for the purpose of debugging or logging results. It is a tool or web tool which may be used by developers to debug their code. With the help of the console, information can be displayed about the code that could be values of the variables, any result of a particular expression, or the return value of a function call. Moreover, it can be used to display errors and warnings.

The console is available in every scope since it is a global variable. In the browser window, it can be used as the ‘window.console’ or simply ‘console’ will work and in the browser window, it is present in the global scope.

There are various console object methods, let's see each of them one by one −

assert() Method

If the assertion is false then this method will show a message otherwise no message will be displayed there in the console. Let’s see its syntax first and then move to its code −

console.assert(expression, message);

In the above syntax, we can see there are two parameters passed to the assert function, one is the expression and another is the message.

Expression is the expression that will be checked for the assertion of the message to the console. If the output of the expression is false then the message will be displayed on the console otherwise nothing will happen.

The message is the message that the user wants to display on the console.

clear() Method

Due to the many messages present in the console, it may become cumbersome, to remove all the messages from the console we can use the clear() method which works exactly as its name. Let’s see its syntax.

console.clear();

After the above expression will be encountered the console will be cleared and there may or may not be a message that appears to indicate that the console is cleared. Google shows this message while in Firefox no returned message will be generated.

count() Method

count() method in JavaScript prints the message in the console, and the printed message will be labeled and passed to the count method and ends with the number which indicated the number of times the count method is called. Let’s see the syntax of the count() method −

console.count(label);

Here, the label is the message that will be printed in the console, also if the user doesn’t send any label then by default “default” will be printed as the message.

error() Method

As the name suggests the error() method prints the error on the console which is nothing but a message which can be printed on the console for the purpose of testing a program, but the best part of the error method makes it better as compared to other methods is it prints the message in the red color.

console.error(message);

Here, the message is an error message which is provided by the user and printed in red color on the console.

group() Method

group() Method is used to create a new inline group in the console. As all the console messages appear in levels, it provides an extra or additional level and all the new messages appear in that same level until the groupEnd() method is called. Let’s see the syntax of the group() Method −

console.group(label)

In the above syntax, label is the message which is not necessary or more specifically the compulsory parameter to pass.

groupEnd() Method

groupEnd() Method of the console is nothing but the method to end the group created using the group() method. Using group() method we can start a new inline group and to close that group we can use the groupEnd() Method. Let’s set its syntax −

console.groupEnd()

There is no parameter passed to this method, hence nothing will be printed on the console.

info() Method

To give any special information on the console the info() method is used. On some browsers there may be any special icon appears in front of the message printed using the info() method, like in the firefox browser a small ‘i’ icon will be present. Although, on many browsers it may not present and a simple message is present instead. Let’s see its syntax −

console.info(message)

Here message is the special message that user wants to print in the console as the information and by using the info() method, user may give some extra information through the code and make code more readable.

log() Method

log() method is one of the most famous method and most of the people use it because it is very simple and most of data types of JavaScript programming language can be printed using the log() method at the console. Array, string, boolean, object, etc all the data types of JavaScript programming language can be printed on the console. Let’s see its syntax −

console.log(message)

In the above syntax, the message could be a string, array, object, etc. log() method is mainly used for the testing purpose mostly.

table() Method

As per the name, table method of the console is used to print the data in the table format in the console. This makes the data look clear on the console and to present the data in the specific manner so it becomes more readable. Let’s see its syntax −

console.table(tabledata, tablecolumns);

In the above syntax, we have passed two parameters, first parameter ‘tabledata’ is compulsory and contains the data to present and second parameter is optional and if not given then the column names are taken by default starting from zero.

time() Method

time() method is used to start a timer to get the idea how long a testing process lasts. It will start a timing in the console and can be seen there. Let’s see its syntax −

console.time(label)

In the above syntax, label is the name given to the timer, so we can get unique type of timers in the console.

timeEnd() Method

To end a timer we can use the timeEnd() method and it will end the timer. Let’s see its syntax −

console.timeEnd(label)

In the above syntax, label is the name of the timer which is initially started and now user wants to stop.

Conclusion

In the above tutorial we have seen that a console is an object in the JavaScript programming language that is used for the purpose of debugging or logging results. It is a tool or web tool which may be used by developers to debug their code. We have seen many methods of the console including the log(), info(), table(), time(), group(), error(), count(), clear(), and time(). log() method is the one of the most commonly used method present while info() method is used to give the information to the console.

Updated on: 17-Mar-2023

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements