Some of the ways you can debug a script include:
- Adding debugging code to your program
ADDING DEBUGGING CODE TO THE PROGRAM
Another method used in checking for bugs is with the use of debugging codes. We can easily add codes such as the alert() and document.write() methods to the program to alert us on any bugs found in the program.
var debug = true; var TheImage = "widget"; if( debug ) alert( "Calls swappingImage() with argument: " + TheImage ); var swapStatus = swappingImage( TheImage ); if( debugging ) alert( "Exits swappingImage() with swapStatus=" + swapStatus );
HOW TO REDUCE ERRORS WHEN CODING
- Avoid writing long script of codes without double checking if they are correct.
- Always use meaningful comments at all time to remind yourself why you wrote a line of code.
- Maintain high consistence in your variable naming.
- Be mindful of your quotation marks and and equal signs.
- Always remember to declare your variables before using them.