To add debugging code to JavaScript, use the alert() or document.write() methods in your program. For example,
var debugging = true; var whichImage = "widget"; if( debugging ) alert( "Calls swapImage() with argument: " + whichImage ); var swapStatus = swapImage( whichImage ); if( debugging ) alert( "Exits swapImage() with swapStatus=" + swapStatus );
Examine the content and order of the alert() as they appear, you can examine the health of your program very easily.