Errors when writing js code are a headache. For a js novice who has just started, js errors may occur. I've been on it for a long time, and my mentality has jumped. Let’s take a look at this article
Common js error types
(1) SyntaxError
SyntaxError is a syntax error that occurs when parsing the code
// Variable name error
var 1a;
// Missing brackets
console.log 'hello');
(2) ReferenceError
ReferenceError is when a variable that does not exist is referenced An error occurred.
unknownVariable
// ReferenceError: unknownVariable is not defined
Another trigger scenario is to assign a value to an object that cannot be assigned, such as the running result of a function Or this assignment.
console.log() = 1
// ReferenceError: Invalid left-hand side in assignment
this = 1
// ReferenceError: Invalid left-hand side in assignment
The above code assigns the running result of the function console.log and this, and the result causes a ReferenceError error
(3) RangeError
RangeError is An error that occurs when a value is outside the valid range. There are several main situations, one is that the array length is a negative number, the other is that the method parameters of the Number object are 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
(4) TypeError
TypeError is an error that occurs when a variable or parameter is not of the expected type. For example, if you use the new command on primitive types such as strings, Boolean values, and numerical values, this error will be thrown because the parameter 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
In the second case of the above code, calling a method that does not exist on the object will throw a TypeError.
(5) URIError
URIError is an error thrown when the parameters of URI-related functions are incorrect, mainly involving encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent(), escape () and unescape() these six functions.
decodeURI('%2')
// URIError: URI malformed
(6) EvalError
When the eval function is not executed correctly, it will Throws an EvalError. This error type no longer appears in ES5 and is only retained to ensure compatibility with previous code.
The above six derived errors, together with the original Error object, are all constructors. Developers can use them to artificially generate instances of error objects.
new Error("An error occurred!");
new RangeError("An error occurred, the variable exceeds the valid range!");
new TypeError("An error occurred, the variable type is invalid!") ;
The above code represents a new instance of the error object. The essence is to manually throw the error. As you can see, the constructor of the error object accepts a parameter, which represents the error message.
Related recommendations:
Front-end JS interview questions
Native js implements movable prompt div box source code
html/css/js Chinese manual suitable for PHP beginners and programmers
The above is the detailed content of Summary of common JS error types. For more information, please follow other related articles on the PHP Chinese website!