1) syntaxerror
SyntaxError is a syntax error that occurs when parsing code
// 变量名错误 var 1a; // 缺少括号 console.log ‘hello‘);
(2) Referenceerror
Referenceerror is an error that occurs when a variable that does not exist is referenced.
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 result of the above code on the function Console.log and this assignment results in a referenceerror error
(3) Rangeerror
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
(4) TypeError
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
In the second case of the above code, the method that calls the object does not exist throws a typeerror error.
(5) Urierror
Urierror is an error that is thrown when the parameters of a URI-related function are incorrect, mainly involving encodeURI (), decodeURI (), encodeURIComponent (), decodeURIComponent (), Escape () and unescape () these six functions.
decodeURI(‘%2‘) // URIError: URI malformed
(6) Evalerror
An evalerror error is thrown when the eval function is not executed correctly. 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("出错了!");
new RangeError("出错了,变量超出有效范围!");
new TypeError("出错了,变量类型无效!");
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).
"Repost" JS common error types