Javascript error handling and debugging

Source: Internet
Author: User

Javascript error handling and debugging


This article mainly introduces some common errors in javascript and some basis for debugging and handling errors to help us improve the quality of javascript code.


Let's startJavascriptCommon7Errors:


1.Variable undefined

The following statement implicitly creates a new variable abc and assigns the value 123 to the variable:


abc = 123;

Strictly speaking, the variable should be explicitly defined:


var abc = 123;

(In fact, whether to use the var keyword is related to the scope of the variable, so it is best to add the var keyword when defining the variable)

However, if an undefined variable is used, an error occurs. For example, if abc is not explicitly or implicitly defined in advance, the following statement produces an error:



2.Case Sensitive

Case-sensitivity errors are one of the most common errors, but are sometimes difficult to find. For example, find the three errors in the following statement:


var myName ="Paul";If(myName=="paul") {    alert(myName.toUppercase());} 


3.Unmatched braces

4.Unmatched parentheses

(The above two errors are similar. When the statement is too long, it will happen. Of course, these are also very easy to find .)


5.The plus sign is missing when the string is connected.(+)

This type of error is easy to detect in a short time, but it will be very troublesome if it is too long, and different browser Error alerts are different. for ie browsers, the message "Error: Expected (Object Missing)" is displayed. For Firefox browsers, the message "Missing; before statement (number Missing in the following statements)" is displayed )".


6.Assign values instead of equal values

See the following code:


var myNumber = 99;if (myNumber=101) {    alert("myNumber is 101");}else {    alert("myNumber is"+myNumber);}


After reading the above Code, do you want the alert () method in the else statement to be executed and prompt "myNumber is 99 "? But this is not the case. if statements are incorrectly judged, the two equal signs (=) are written as one (=), and the comparison operator (=) is written as the value assignment operator (= ). this is a very typical error. javascript is different from vb. For vb, the value assignment and comparison operations are all equal signs. this error does not generate error information, which is easily ignored. we should remember that when the logic of the program is chaotic and abnormal, we can check whether there is a problem.



// Equal and full alert (1 = '1'); // true, equal to the value, the type does not need to be compared with alert (1 = '1 '); // false; comparison type alert (1 = true); // true, 1 is implicitly converted to a Boolean value, true = truealert (1 = true); // false, 1 is a numerical value, and true is a Boolean value.


PS: When the types are not equal, we recommend that you use full equal =



7.Mistaken methods for Attributes,Or mistake attributes for methods.

Here, we often forget to include a pair of parentheses behind the method name when using the method, or include extra parentheses after the attribute name when using the attribute. For example, the following code:


var nowDate = new Date;alert(nowDate.getDay);


In the first row, the constructor of the Date object is used. This constructor is a simple method of the Date object. However, there is no parentheses after the method name. In the second row, call the getDay () method of the Date object, but forget one.


The correct code is as follows:


var nowDate = new Date();alert(nowDate.getDay());


Error Handling

1. try... catch statement

Try... catch statements always appear in pairs.

Significance of try-catch

1. You can correct the error by modifying the code without using try-catch.

2. For browser compatibility problems, you can determine whether the browser supports a certain attribute or method without the need for try-catch.


For example, an error may occur when the Code cannot be modified. In this case, try-catch is used to interrupt the network.



try {alert(innerWidth);                        //W3C}catch (e) {alert(document.documentElement.clientWidth);                        //IE}


PS: the compatibility is true, but the logic is incorrect. Because innerWidth does not support browsers, it may not be IE.


// If an error is thrown, it means that we cannot solve the problem by ourselves, so we need to report the error



Try {new10;} catch (e) {if (einstanceof TypeError) {thrownew TypeError ('Type error: An error may occur when new is instantiated! ');} Else {// alert (e); // this kind of behavior is called processing error. The browser does not hold the error because it has been processed, blocked Error display thrownew Error ('unknown Error! ') ;}} New10; // the browser throws the addEvent (window, 'error', function () {alert (' program error! ');});


Script Debugger

Examples/u5tNGhvLS/examples + m/samples + samples/2 NbGzKi/ydLUsum/tKGjsum/samples + samples/logs/cHL5K/AwMb319S0 + LXEo6y/samples + samples/LK7tau/expires + m/9 s/second + LP2zOHKvqGjy/Second/qsb0y/Second/second + d2VivOyy6cb3o/3P1Mq + tO3O87/Second/rLYxvDAtKGjtPK/Second/second + sum/ tNK7o76/qrei1d + ceiling/ceiling + m/ceiling + Gxu9L + stjG8MC0oaO08r + ceiling/2 NbGzKihow = "height =" 368 "src =" "width =" 714" />

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: 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.