Here are some effective methods when we debugging with consoles hoping to make it easier to debugging.
Some tips to help debugging with consoles are easier
- Tram Ho
During the coding process we probably need a lot of console support for debugging. One of the most useful and popular commands that any programmer knows is console.log
. But there are many other methods that support with a variety of features and a clear display interface for easier debugging
that you may not know about. In this article I will share some effective methods with the console
in the hope of helping a lot of you in the debugging
process.
1 Console.log ()
This is the basic method for displaying a string or a value on the console screen. For example, we have two objects in turn
1 2 | var obj1 = {a: 1, b: 2} var obj2 = {a: 2, b: 3} |
and we want to display their values for comparison, instead of using 2 statements, we can use console.log(obj1, obj2)
to easily compare each attribute easily and reduce command line minority.
2 Console.table ()
Another effective way to observe the values in objects is easier by displaying them in table form.
3 Console.trace ()
Javascipt is a tightly structured language, so it’s difficult to know what the timing of functions is, especially when we’re debugging someone else’s code. Don’t worry, let console.trace()
do it. The following example will help us show the trace how the function executes.
1 2 3 4 5 6 7 8 9 | function a () { b (); } function b () { console.trace (); } a (); |
4 Console.group ()
console.group()
will be useful when we want to group logs related to each other to make it easier to observe.
1 2 3 4 5 6 7 | console.group ('company') console.log ('name: sunset') console.log ('address: 16 ly thuong chi') console.group ('members') console.log ('some members') console.groupEnd () console.groupEnd () |
5 Console.time ()
To know how long it takes to execute a block, especially loops, this is a very effective method. For example
1 2 3 4 5 6 | console.time ('Timer'); var items = []; for (var i = 0; i <100000; i ++) { items.push ({index: i}); } console.timeEnd ('Timer'); |
6 Console.count ()
In case the function runs multiple times, you can count its running times with console.count()
. For example
1 2 3 4 5 | for (i = 0; i <10; i ++) { a (); } function a () {console.count (); } |
Source : viblo