views:

897

answers:

20

Assume that you have a problematic code snippet. What is your way to debug it?

Are you alert the variables?

Do you use Visual Studio .NET or any other tool to debug your javascript snippet?

Are there good script libraries that ease the debugging process, or do you write your own?

+50  A: 

FireBug, is one of the most popular tools for this purpose.

Ryan Oberoi
Link it up: http://getfirebug.com/
Adam Backstrom
I like firebug, but I wouldn't claim it to be head and shoulders above webkit's inspector.
rpflo
Firebug was ahead of it's time when it came out, but I don't think it stands as the best tool, given other tools that have come out recently.
James
Peer Pressure. Changed the claim from "One tool: FireBug, stands head and shoulders above the rest." to "one of the most popular".
Ryan Oberoi
@Ryan- you got scared
TStamper
Hey, it pays to be nice on a community forum. I got my second Badge of Honor: Nice Answer for this question... but no reputation points though. It would be fun if Stack Overflow opened their source or at least their design and algorithms.
Ryan Oberoi
I'm using Firebug since I discovered it, and it helps me a lot! console.debug, profiler, inspector...
Julio Greff
Oh boy, I think I hit the daily cap on reputation points. I did not receive any points on the popular answer. What a waste of my deep knowledge in javascript debugging tools... :)
Ryan Oberoi
When I installed firebug it was like getting a jetpack for my webdev
larson4
+1 firebug rules
solomongaby
+1  A: 

I found the new version of IE 8 ( Press F12) is very good to debug JavaScript code.

Of course, FireBug is good if you use FireFox.

J.W.
+4  A: 

I use Firebug. But if I'm working on a web app that isn't compatible with FireFox I just use the debugger statement and debug in VS.NET.

George
+4  A: 

Firebug, the new IE8, and of course, the trusty alert function :-D.

Colin
I admin to relying on alert way too often!
ScottE
For a quick and dirty check i have no problem using it.
Colin
+3  A: 

Stumbled onto this earlier this week, kinda cool... JS Bin

mbmccormick
Nice innovation!
Thomas L Holaday
+3  A: 

Visual Studio 2008 has some very good Javascript debugging tools. You can drop a breakpoint in your client side JS code and step through it using the exact same tools as you would the server side code. No need to attach to a process or or do anything tricky to enable it.

JohnFx
+2  A: 

I use Webkit's developer menu/console (Safari 4). Almost identical to FireBug.

console.log() is the new black--far better than alert().

rpflo
+4  A: 

I’m using Safari’s Web Inspector.

Gumbo
+2  A: 

I use a few tools: Fiddler, Firebug, & Visual Studio. I hear IE8 has a good built-in debug.

Cheers

d34dIO
By "Fiddler" do you mean Fiddler Web Debugger?
Thomas L Holaday
+8  A: 

Start with Firebug, IE Debugger.

Be careful with debuggers in Javascript though. Every once in a while they will affect the environment just enough to cause some of the errors you are trying to debug.

EDIT

One comment requested examples:

For IE it's generally a gradual slowdown, some kind of memory leak type deal. After a half hour or so I need to restart. Seems to be fairly regular.

For Firebug, It's probably been more than a year so it may have been an older version. As a result, I don't remember the specifics, but basically the code was not running correctly and after trying to debug it for a while I disabled firebug and the code worked fine.

Tom Hubbard
Can you give an example?
finnw
+1  A: 

alert(...);

Nick
A: 

I like firebug - it allows you to put breakpoints in your script and then you can step through, you can add watches, it even shows the stack. Awesome thing.

alert() is something which is also quite handy.

cheers

Andriyev
+1  A: 

I used to use FireBug, until IE8 came out, I'm not a huge fan of IE, but after spending some time with the built-in developer tools, which includes a really nice debugger, it seems pointless to use anything else. I have to tip my hat to Microsoft they did a fantastic job on this tool.

James
For basic debugging, the IE8 debugger has suited most of my needs. However, if you are doing any sort of performance testing, you will quickly find IE lacking.I had a project recently that utilized some heavy javascript, and we really needed to trim things down for inferior systems, as we were running into the dreaded "unresponsive script error". Firebug was invaluable in this instance, because I was able to run the console.profile() method to figure out where all of my time was being spent.
Robotsu
The IE8 debugger also has a profile feature (albeit not as graphical as FireBug) that provides call tree, call count and time spent on each method. I've found this adequate in isolating which JS code is taking too long.
James
+2  A: 

You might also check out YUI Logger. All you have to do to use it is include a couple of tags in your html. It is a helpful addition to Firebug, which is more or less a must.

Rob Lund
Doesn't jQuery have similar functionality?
shapr
+2  A: 

My first step is always to validate the html and to check syntax with JSLint. If you have clean markup and valid javascript then it is time for Firebug or another debugger.

Ken
+1 - Incredibly useful when you can't actually use a debugger.
sheepsimulator
+24  A: 
  • IE8 (Developer Tools - F12) Anything else is second rate in IE land
  • FireFox+Firebug (getfirebug.com) Hit F12 to display.
  • Safari (Show Menu Bar, Preferences->Advanced->Show Develop menu bar)
  • Google Chrome JavaScript Console (Ctrl-Shift-J) Mostly the same browser as Safari, but Safari is better IMHO.
  • Opera (Tools->Advanced->Developer Tools)
Chris Brandsma
+1 opera js debugger gives a better error message then all the rest
solomongaby
A: 

As with most answers, it really depends: What are you trying to achieve with your debugging? Basic development, fixing performance issues? For basic development, all the previous answers are more than adequate.

For performance testing specifically, I recommend Firebug. Being able to profile which methods are the most expensive in terms of time has been invaluable for a number of projects I have worked on. As client-side libraries become more and more robust, and more responsibility is placed client-side in general, this type of debugging and profiling will only become more useful.

Firebug Console API: http://getfirebug.com/console.html

Robotsu
+3  A: 

Although Alert(msg); works in those "I just want to find out whats going on" scenarios... every developer has encountered that case where you end up in a (very large or endless) loop that you can't break out of.

I'd recommend that during development if you want a very in-your-face debug option, use a debug option that lets you break out. (PS Opera, Safari? and Chrome? all have this available in their native dialogs)

//global flag
_debug = true;
function debug(msg){
  if(_debug){
    if(!confirm(msg + '\n\nPress Cancel to stop debugging.')){
      _debug = false;
    }
  }
}

With the above you can get your self into a large loop of popup debugging, where pressing [Enter]/[Ok] lets you jump through each message, but pressing [Escape]/[Cancel] lets you break out nicely.

scunliffe
A: 

Beside using Visual Studio's Javascript debugger, I write my own simple panel that I include to a page. It's simply like Immediate Window of Visual Studio. I can change my variables' values, call my functions, and see variables' values. It simply evaluates the code written in the text field.

Canavar
+1  A: 

I'm using Venkman, JavaScript debugger for XUL applications.

http://www.mozilla.org/projects/venkman/

kuy