I come from Java Background and so used to Debugging using Eclipse but have recently started on JavaScript(jQuery in particular) and am having really hard time debugging JavaScript Code so my question is
What are the best ways of Debugging JavaScript ?
I have tried using Firebug and it is good, but wanted to know
If we have any other useful tools or stratergies for Debugging JavaScript ?
I've recently switched from Firebug to Google Chrome as it has some pretty powerful debugging tools built-in (Ctrl+Shift+J to get the Developer Tools window open), including break-points.
I've also used logging/tracing to great effect. Some examples:
Modern browsers include built-in debugging tools. In IE, hit F12 (Tools > Developer Tools), go to the Script tab and hit Start Debugging. It will break on an error and you can set breakpoints. I've found that IE8's Developer Tools are extremely useful.
In Chrome, it's Ctrl+Shift+J (Page > Developer > JS Console) and click the pause stop sign ("pause on exceptions") button. You can also set breakpoints.
I rarely use a debugger and tend to prefer logging, for which I use my own log4javascript. It works consistently in all mainstream browsers, including IE 6 (and in fact IE 5 and 5.5), and by default displays logging messages in a separate console window, which allows you to filter log messages by severity, search log messages (optionally using a regular expression) and more. It can also send log messages to the server using Ajax.
Example 1: Hello world
var log = log4javascript.getDefaultLogger();
log.info("Hello world");
displays
19:52:03 INFO - Hello world
Example 2: Logging an error with a message
try {
throw new Error("Faking something going wrong!");
} catch (e) {
log.error("An error occurred", e);
}
displays
19:52:32 ERROR - An error occurred
Exception: Faking something going wrong! on line number 80 in file basic.html
Example 3: Logging multiple messages with one logging call
var a = "Hello";
var b = 3;
log.debug(a, b);
displays
19:53:05 DEBUG - Hello 3
Example 4: Logging an object
var obj = new Object();
obj.name = "Octopus";
obj.tentacles = 8;
log.info(obj);
displays
19:53:17 INFO - {
name: Octopus,
tentacles: 8
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With