How can I add debugging code to my JavaScript?


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.

Updated on: 17-Jan-2020

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements