Home Web Front-end JS Tutorial Sharing six error types in JavaScript

Sharing six error types in JavaScript

Feb 05, 2018 am 10:21 AM
javascript js share

The console error information in js is mainly divided into two categories. The first category is syntax errors. If this type of error is encountered during the pre-parsing process, the entire js file will not be executed. Another type of error is collectively called an exception. This type of error will cause the code after the line where the error occurs to be unable to execute, but the code before that line will not be affected.

1. SyntaxError: Syntax error

// 1. Syntax Error: 语法错误
// 1.1 变量名不符合规范
var 1       // Uncaught SyntaxError: Unexpected number
var 1a       // Uncaught SyntaxError: Invalid or unexpected token
// 1.2 给关键字赋值
function = 5     // Uncaught SyntaxError: Unexpected token =
Copy after login

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 a function or a function assignment.

// 2.1 引用了不存在的变量
a()       // Uncaught ReferenceError: a is not defined
console.log(b)     // Uncaught ReferenceError: b is not defined
// 2.2 给一个无法被赋值的对象赋值
console.log("abc") = 1   // Uncaught ReferenceError: Invalid left-hand side in assignment
Copy after login

3. RangeError: Range Error

RangeError is an error that occurs when a value is only outside the valid range. There are several main situations. The first is that the array length is a negative number, the second is that the method parameters of the Number object are out of range, and the function stack exceeds the maximum value.

// 3.1 数组长度为负数
[].length = -5      // Uncaught RangeError: Invalid array length
// 3.2 Number对象的方法参数超出范围
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
// 说明: toFixed方法的作用是将数字四舍五入为指定小数位数的数字,参数是小数点后的位数,范围为0-20.
Copy after login

4. TypeError type error

An error that occurs when a variable or parameter is not of the expected type. For example, using new primitive types such as strings and Boolean values ​​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 调用不存在的方法
123()        // Uncaught TypeError: 123 is not a function
var o = {}
o.run()        // Uncaught TypeError: o.run is not a function
// 4.2 new关键字后接基本类型
var p = new 456      // Uncaught TypeError: 456 is not a constructor
Copy after login

5. URIError, URL error

The main reason is that the parameters of the related functions are incorrect.

decodeURI("%")     // Uncaught URIError: URI malformed at decodeURI
Copy after login

Errors thrown when URI related parameters are incorrect, mainly involving six functions: encodeURI, decodeURI(), encodeURIComponent(), decodeURIComponent(), escape() and unescape().

6. EvalError eval() function execution error

In JavaScript below ES5, when the eval() function is not executed correctly, an evalError error will be thrown.

For example, the following situation:

var myEval = eval;
myEval("alert('call eval')");
Copy after login

It should be noted that this error is no longer thrown in JavaScript above ES5, but you can still customize this type of error prompt through the new keyword .

The above 6 derived errors, together with the original Error object, are all constructors. Developers can use them to generate instances of error objects.

new Error([message[,fileName[,lineNumber]]]),
Copy after login

The first parameter represents the error message, the second is the file name, and the third is the line number.

Related recommendations:

Summary of common JS error types

php Detailed explanation of error types and shielding methods

Common php error types and shielding methods

The above is the detailed content of Sharing six error types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

How to share Quark Netdisk to Baidu Netdisk?

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

How to share files with friends on Baidu Netdisk

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Mango tv member account sharing 2023 Mango tv member account sharing 2023 Feb 07, 2024 pm 02:27 PM

Mango tv member account sharing 2023

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

Share two installation methods for HP printer drivers Share two installation methods for HP printer drivers Mar 13, 2024 pm 05:16 PM

Share two installation methods for HP printer drivers

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

See all articles