Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the skills of using console for JS debugging

Shulou Source: shulou.com Published: 2022-06-02 15:13:40 09月11日 Update

This article mainly explains "what are the skills of using console for JS debugging". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the skills of using console for JS debugging".

Tip # 1 console.trace ()

If you want to get the prompt location of the log information, you can get a stack trace with recorded data by using console.trace ().

Tip # 2 console.time () & & console.timeEnd ()

If you are trying to find a more hidden performance problem, use console.time () to start calculating the time, and then use console.timeEnd () to print.

Tip # 3 console.memory

If your performance problems are more intractable and you are looking for a hidden memory leak, you may want to try and use console.memory (properties, not functions) to check your heap size status.

Tip # 4 console.profile ('profileName') & console.profileEnd (' profileName')

This is not a standard approach, but it is widely supported. You can use console.profile ('profileName'), and then use console.profileEnd (' profileName') to launch and end the browser performance tool-"performance profile" from the code. This will help you analyze exactly what you want and prevent you from clicking the mouse, which depends on the execution time of the program.

Tip # 5 console.count ("STUFF I COUNT")

In cases where functions or code are repeated, you can use console.count ('?') To calculate the number of times your code has been read.

Tip # 6 console.assert (false, "Log me!")

Yes, conditional logging does not wrap your log in if-else:)

You can use console.assert (condition, msg) to record something when condition is false.

Disclaimer: in Node.js, this throws an Assertion Error!

Tip # 7 console.group ('group') & console.groupEnd (' group')

After writing so many journals, you may want to organize them. A small but useful tool is console.group () and console.groupEnd (). Using console groups, the console logs are organized together, and each group creates another level in the hierarchy. Call groupEnd () to decrease one level (go back to the previous level).

Tip # 8 String substitutions

When logging, you can replace merge variables with strings. These references are (% s = string,%i = integer,%o = object,%f = float).

Tip # 9 console.clear ()

So, after writing so many logs, it's time to clean up your console.

Tip # 10 console.table ()

In my opinion, this is a real "treasure"! You can use console.table () to print a very beautiful form!

Thank you for your reading, the above is the content of "what are the skills of using console for JS debugging". After the study of this article, I believe you have a deeper understanding of the skills of using console for JS debugging, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Logs tips content code performance console problems learning control functions tools situations time usefulness levels can think tricky accurate this is not memory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Docker vpn MariaDB