Six types of errors in JavaScript


Just entering the front-end pit, students who are not very good in English, are they still scratching their heads for the mistakes of the console? Today I will show you the common types of errors in JavaScript.

The console error messages in js are mainly divided into two categories. The first category is syntax errors. If such errors are encountered during the pre-parsing process, the entire js file cannot be executed. Another type of error, collectively called exceptions, causes the code after the line where the error occurs to fail to execute, but the code before that line is not affected.

1. SyntaxError: Syntax error

// 1. Syntax Error: Syntax error
// 1.1 Variable name does not conform to specification
var 1 // Uncaught SyntaxError: Unexpected number
var 1a // Uncaught SyntaxError: Invalid or unexpected token
// 1.2 Assign value to keyword
function = 5 // Uncaught SyntaxError: Unexpected token = 
2. Uncaught ReferenceError: reference error An error that occurs when

referencing a variable that does not exist. Assign a value to an object that cannot be assigned, such as the result of running a function or assigning a value to a function.

// 2.1 Reference to non-existent variable
a() // Uncaught ReferenceError: a is not defined
console.log(b) // Uncaught ReferenceError: b is not defined
// 2.2 Assignment to an object that cannot be assigned
console.log("abc") = 1 // Uncaught ReferenceError: Invalid left-hand side in assignment
3. RangeError: Range Error

RangeError is an error that occurs when one is only outside the valid range. There are several main cases, the first is that the length of the array is negative, the second is that the method parameter of the Number object exceeds the range, and the function stack exceeds the maximum value.

// 3.1 Array length is negative
[].length = -5 // Uncaught RangeError: Invalid array length
// 3.2 Method parameter of Number object is out of range
var num = new Number(12.34)
console.log(num.toFixed(-1 )) // Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
// Description: The function of the toFixed method is to round the number to the specified number of decimal places, the parameter is the number of digits after the decimal point, The range is 0-20.
4. TypeError type error

An error that occurs when a variable or parameter is not of the expected type. For example, using primitive types such as new strings, booleans, and calling methods that do not exist in the object will throw this error, because the parameter of the new command should be a constructor.

// 4.1 Calling non-existent method
123() // Uncaught TypeError: 123 is not a function
var o = {}
o.run() // Uncaught TypeError: o.run is not a function
// 4.2 After new keyword Connect the basic type
var p = new 456 // Uncaught TypeError: 456 is not a constructor
5. URIError, URL error

Mainly because the parameters of related functions are incorrect.

decodeURI("%") // Uncaught URIError: URI malformed at decodeURI
Error thrown when the relevant parameters of decodeURI URI are incorrect, mainly involving encodeURI, decodeURI(), encodeURIComponent(), decodeURIComponent(), escape() and unescape() six a function.

6. EvalError eval() function execution error

In JavaScript below ES5, when the eval() function is not executed correctly, an evalError will be thrown.
For example, the following situation:

var myEval = eval;
myEval("alert('call eval')");
It should be noted that this error is no longer thrown in JavaScript above ES5, but it can still be customized through the new keyword An error message of this type.

The above six derived errors, along with the original Error object, are all constructors. Developers can use them, thinking that an instance of the wrong object is generated.

new Error([message[,fileName[,lineNumber]]]),
the first parameter is the error message, the second is the file name, and the third is the line number.
more 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326239579&siteId=291194637