We tend to stay within our comfort zone, but it is worth expanding our comfort zone occasionally, using unusual and useful techniques.
- 10 Tips for Javascript Debugging Like a PRO with Console
- Translator: Fundebug
To ensure readability, free translation rather than literal translation is used in this paper. In addition, the copyright of this article belongs to the original author, and translation is for study only.
One of my biggest passions over the past decade has been front-end development (specifically JavaScript). As a craftsman, I like to specialize in tools. In this article, I’ll show you some tricks for debugging with the old console.
Yes, we all know the basic tips:
The console. The log (" Hello World! '); The console. The info (' Something happened... '); The console. Warn (' Something strange happened... '); The console. The error (' Something latest happened... ');Copy the code
From now on, I will teach you some skills you didn’t know, so that you can become an old driver!
1. console.trace()
If you want to know where the message was printed, use console.trace() to get the stacktrace of the data to print.
2. console.time() && console.timeEnd()
If you want to analyze the performance of a function, you can use console.time() to time it, console.timeend () to end it, and the console prints out the time difference between the two.
3. console.memory
If you find performance problems difficult to analyze and may also consider whether there is a memory leak, you can use console.memory (note that memory is a property of console, not a function) to check the current heap usage.
Fundebug helps you debug better, welcome to try!
4. The console. Profile (” profileName “) and the console. ProfileEnd (” profileName “)
Although not a standard practice, it is widely accepted. You can use these two commands to start and stop profiling. This will help you do precise profiling in your code. Instead of relying on manual mouse clicks. You can find the profile in the browser console Javacript Profiler.
5. The console. The count (” STUFF I count “)
Sometimes to keep track of how many times a function or piece of code is executed repeatedly, you can use console.count(‘? ‘) to record. Each time the code is executed, it automatically increments by 1.
6. The console. Assert (false, “Log me!” )
Instead of using if-else, you can use console.assert to output messages under certain false conditions. Assertion Error inside Node.js
7. The console. Group (” group “) and the console. GroupEnd (” group “)
If you want to do a formatted collation of the printed log, use console.group() and console.groupend (). Console. group lets you aggregate logs into groups and form nested hierarchies. Take an example:
8. String substitutions
You can use console.log to print variables (%s = string, % I = INTEGER, % O = Object, %f = float).
9. console.clear()
We’ve already printed a lot of records on the console, so use console.clear() to clear them.
10. console.table()
Last one! You can print the object as a table using console.table().
Are your users experiencing bugs?
Experience the Demo
Free to use