1. Debugging with the alert () statement
When you cannot locate the exception that is thrown by a program error, you can use code tracing to find the error, and you can place the alert () statement in a different location in the program, using it to display variables and function return values in your program.
<script language= "JavaScript" >
Alert ("Start");
var a= "2";
var b= "3";
Alert ("Execute");
Alert ("A+b");
Alert ("End");
</script>
2. Debugging using the Write () statement
Sometimes you want to put all the debugging information in a list on the page, and you can use the write () method to debug.
<script language= "JavaScript" >
document.write ("Start");
var a=1;
var b=2;
document.write ("Execution");
document.write ("A+b");
document.write ("End");
</script>
3. Debugging with a custom exception message
Throwing a custom exception is the best path to debugging JavaScript code. It uses the try...catch...finally statement and the throw mechanism.
<meta http-equiv= "Content-type" content= "text/html"; charset= "UTF-8" >
<title> throw a custom exception </title>
<script language= "JavaScript" >
function test (num1,num2) {
try{
if ((arguments). length<2) {
throw new Error ("Insufficient number of parameters"); Throw Custom Exception information
}
if (num1/num2== "Infinity") {
throw new Error ("divisor cannot be 0");
}
}catch (Exception) {
alert (exception.message); Pop-up warning dialog box
}
}
</script>
<body>
<script language= "JavaScript" >
Test (1);
Test (1,0);
</script>
</body>
JavaScript language Debugging Tips