JavaScript language Debugging Tips

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.