10 JavaScript Console Methods

A JavaScript method is a property containing a function definition. A Console method is an object used to access browsers debugging console. With the help of console methods, we can print messages, warning, and errors to browser console, which is helpful for debugging purpose.

The developer console in a web browser is a tool which logs the information associated with a web page, such as JavaScript, network requests, and security errors etc. To open the developer console window on Google Chrome, use the keyboard shortcut Ctrl + Shift + J (on Windows) or Ctrl + Option + J (on macOS).

The console object provides multiple methods to use. In this tutorial, you will learn uses of JavaScript console methods.

  1. LOG
  2. INFO
  3. WARN
  4. ERROR
  5. ASSERT
  6. COUNT and COUNTRESET
  7. TIME, TIMELOG and TIMEEND
  8. GROUP and GROUPEND
  9. TABLE
  10. CLEAR

Here is basic details about all the above listed function and uses.

1. LOG

Use console.log() method to print a message on web console. It can print a string or any number of JavaScript objects. Instead of displaying anything on browser console, it logs messages to a debugging console.

console.log("Hello World");
console.log("Msg", obj1, obj2);

2. INFO

The console.info() function is used to display a message to the browser console. This is pretty similar to console.log but developers uses it for permanent messages to output to the console in there code.

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

3. WARN

Use console.warn() function to print warning message on browser console. It takes one argument of message to be displayed.

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

You can also send an object to print on console message. For example:

var myObj = { firstname : "Rahul", lastname : "K" };
console.warn(myObj);

4. ERROR

Use console.error() method to write an error message to the console. It is used for the testing and troubleshooting purpose. It taken one argument of any type like String or Object.

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

Alternatively use:

var myObj = { firstname : "Rahul", lastname : "K" };
console.error(myObj);

5. ASSERT

Use console.assert() method writes a message to the console based on a condition. It print message to console only If the first argument expression evaluate to false.

console.assert(document.getElementById("name"), "No element found with ID 'name'");

6. COUNT and COUNTRESET

The console.count() method is used to write message on console as the number of times it is called.

for (i = 1; i <= 5; i++){
    cosole.count();
}

By default it labled message with “default”, But you can change the label by passing it as argument.

Use cosole.countreset() method to reset the counter, So you can use this again further.

for (i = 1; i <= 5; i++){
    cosole.count();
}
cosole.countreset();

7. TIME, TIMELOG and TIMEEND

Use console.time() function to start a timer to track how long an operation takes. Before staring operating use time function to start timer and use console.timeEnd() function once the operating finishes. With the help of this you can find the time taken by a specific operating by the application.

You can also use console.timeLog() to print current time in your code. This will not start or end any timer, only print 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

Use console.group() function to make group of message on browser console. This will start grouping of message next to it. Then use console.groupEnd() function to end a group started just before.

console.log("This is the outer level");
console.group("First group");
console.log("In the first group");
console.group("Second group");
console.log("In the second group under first group");
console.warn("Still in the second group");
console.groupEnd();
console.log("Back to the first group");
console.groupEnd();
console.debug("Back to the outer level");

9. TABLE

Use console.table() method to print an object in form of table on browser console.

console.table({'id':1001, 'name':'tecadmin.net'});

10. CLEAR

At the end use console.clear() to clear all the message from browser console. This is good to use at start, so it clears the console first before printing other messages.

console.clear();

Conclusion

Hope this tutorial helps you to understand the uses of JavaScript console methods.

The post 10 JavaScript Console Methods appeared first on TecAdmin.

console General Articles Javascript methods TecAdmin