The most common logging debugging API used by javascript developers is console.log(), but there are many more options to choose from, and I’ll cover them below.

A, the console table ()

Console.table () is a method I highly recommend. It takes JSON or arrays and prints them in table format. It is easy to use and intuitive to visually print JSON objects and arrays.

For example, the following JSON data objects are printed using console.table()

    console.table({
    "id":"1",
    "key":"value",
    "count":2
    });
Copy the code

The console output is as follows:

Or print the array in the following code:

 console.table([
    {
        id: "1",
        key: "value",
        count: 2,
        },
     {
         id: "2",
         key: "value2",
             count: 22,
       },
       {
            id: "3",
            key: "value3",
                count: 5,
               },
     ]);

Copy the code

The console output is as follows:

Second, the console. The error ()

Console.error () is more useful than console.log() in distinguishing error messages from output logs during debugging

As you can see in the figure above, its output is printed in red.

Third, the Time (Time, timeLog, timeEnd)

Console.time (), console.timelog (), and console.timeend () are three methods that are especially useful when we are timing program runtime.

Refer to the figure below to understand these three methods

  • Console.time () is equivalent to the start button in a stopwatch
  • Console.timelog () corresponds to turn-by-turn/point-by-point timing in stopwatch
  • Console.timeend () is the end of the timer
console.time("ForLoop");  
 // "ForLoop" is label here
for (let i = 0; i < 5; i++) {
    console.timeLog('ForLoop'); 
}
console.timeEnd("ForLoop");
Copy the code

The console prints the output

Four,console.warn()

Logs are output in yellow, which makes it easier to view warning logs.

Five, the console. The assert ()

Console. assert(assert_statement,message) is used to set assertions, and if false, the Message message is displayed

if(3! =2){ console.error({ msg1: "msg1", msg2: "msg2" }); } console.assert(3 === 2, {msg1: "msg1", MSg2: "MSg2"});Copy the code

Console. assert(assert_statement,message,args) Another way to format an assertion for output

console.assert(false, "%d nd type for  %s ",2,"console.assert() method");
Copy the code

Sixth, the console. The count ()

Console.count () is especially useful for counting, passing parameters and counting times by parameter labels. The code is as follows:

 for (let i = 0; i < 3; i++) {
   console.count("label");
   console.count();
   console.count(i);
 }
Copy the code

The console prints something like the following

 console.count()  console.count("label")   console.count(i)
 default: 1                label: 1                0: 1
 default: 2                label: 2                1: 1
 default: 3                label: 3                2: 1
Copy the code
  • console.count()If no arguments are passed, the default label is used.
  • Console. countReset(label parameter)You can reset the count of the specified tag to0

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series