Several common types of errors in JavaScript

Source: Internet
Author: User
Tags array length

In general, when we run the code, the error in the console will display your wrong number of lines, find that line, find your corresponding error 1, XXX is not defined XXX no definition 2, XXX is not a function xxx isn't a functional xxx at this time is Unde Fined 3, cannot read property ' xxx ' of undefined cannot read undefined xxx attribute xxx front variable is undefined 4, cannot set property ' xxx ' of nul L cannot give null set XXX property XXX Before the variable is null 5, Invalid or unexpected token punctuation may be Chinese 6, unexpected token A in JSON at position 0 using JSON. Parse parses the JSON string, parsing the content is not legal 7, XMLHttpRequest cannot load Http://XXXXXX. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' Http://XXXXXX ' is therefore not allowed access. When using AJAX to request data, there is a cross-domain 8, illegal break statement an illegal statement 9, SyntaxError (syntax error) syntaxerror is parsing the code when the syntax error//variable name error var 1a; Missing parentheses console.log (' hello '; 10, Referenceerro (reference error) Referenceerror is an error that occurs when referencing a non-existent variable. Unknownvariable
Referenceerror:unknownvariable is not defined another trigger scenario is to assign a value to an object that cannot be allocated, such as the result of running a function or the this assignment. Console.log () = 1
Referenceerror:invalid left-hand side in assignment this = 1
Referenceerror:invalid left-hand side in assignment the above code to the function Console.log run result and this assignment, the result is raised Referenceerror error 11, Rangeerror (range error) Rangeerror is an error that occurs when a value is outside the valid range. There are several main cases, one is the array length is negative, the second is the number object's method parameter out of range, and the function stack exceeds the maximum value. New Array (-1)
Rangeerror:invalid Array length
(1234). toexponential (21)
Rangeerror:toexponential () argument must be between 0 and 20 12, TypeError (type error) TypeError is an error that occurs when a variable or parameter is not the expected type. For example, using the new command for values of primitive types, such as strings, booleans, and numeric values, throws this error because the parameters of the new command should be a constructor. New 123
Typeerror:number is not a func var obj = {}; Obj.unknownmethod ()
Typeerror:undefined is not a function the second case of the above code, the call object does not exist method, will throw TypeError error. 13. Urierror (URI error) Urierror is an error that is thrown when the parameters of a URI-related function are incorrect, mainly involving encodeURI (), decodeURI (), encodeURIComponent (), The six functions of decodeURIComponent (), Escape () and unescape (). decodeURI ('%2 ')//Urierror:uri Malformed 14, Evalerror (eval error) when the Eval function is not executed correctly, a Evalerror error is thrown. The error type is no longer present in ES5, and is only retained to ensure compatibility with previous code. The above 6 derivation errors, along with the original Error object, are constructors. Developers can use them to artificially generate instances of the wrong object. New error ("Error! "); New Rangeerror (" error, variable out of range!) "); New TypeError (" Error, invalid variable type!) "); The code above represents an instance of the new Error object, which is essentially a manual throw error. As you can see, the constructor of the error object takes a parameter that represents the error message (message). 15. Examples of errors
    • The. js file that was introduced cannot be found
The referenced. js file could not be found, chrome error message is as follows: GET file:///D:/JavaBooks/js/jquery%20validation/src-gzh/jquery.validate-1.7.src Net:: Err_file_not_found possible causes:. js file name spelling error (for example, less file suffix. js)
    • js function missing parentheses), function body missing curly braces}
If the function lacks parentheses or the function body is missing parentheses, the browser will prompt an error message when debugging. Here are 2 kinds of situations: 1, the general error messages are reported in the wrong place, this situation is more to say. 2, but sometimes will be reported in other places, this time to understand is that the error message missing brackets This is certain, now is to locate the specific line of error. For example, the Error function/function body is missing parentheses, where the error is not at the end of the function/function body, but is reported in the place where the function is called.
    • CSS code uses//comments, resulting in some div content does not come out, and then do not error
CSS annotations, you can only use/* Comment content */, not double slash//.
    • Multiple variables with the same name defined
The browser will error.
    • Error occurred when the document was not loaded and started to execute JS code
After the document/dom is not loaded, the JS code is executed. For example, the JS code is written in front of the HTML code, and document.getElementById (' traffic ') to access the DOM (that is, the elements of the document), then this time will be reported typeerror:a is null (Firefox browser, Note: A variable is useless, only misleading) or uncaught Typeerror:cannot Read property ' Click2 ' of NULL (Google Chrome).
    • The function of the JS object is undefined
The browser error, said the object's function is undefined, this time can be certain that the current object is not this function. For example, the German map object function is undefined because the map object is covered by a 2.5-D map object. When this bug occurs, the problem is in the function, often because the object is repeatedly defined, or is re-assigned.

Several common types of errors in JavaScript

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.