Home  >  Article  >  Web Front-end  >  What are the types of errors that occur in JavaScript programming? Introduction to error types

What are the types of errors that occur in JavaScript programming? Introduction to error types

青灯夜游
青灯夜游Original
2018-11-10 10:34:494482browse

What are the types of errors that occur in JavaScript programming? This article will introduce you to several common types of errors that occur in JavaScript programming. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. [Related video tutorial recommendations: JavaScript Tutorial]

There are three main types of errors in JavaScript programming, namely: syntax errors, runtime errors, and logic errors.

Let’s take a look at what JavaScript errors will be found during compilation?

1. Syntax Errors

Syntax errors, also known as parsing errors, occur during compilation and compilation of traditional programming languages. When parsing JavaScript.

Example 1, the following line causes a syntax error because it is missing a closing bracket.

<script type="text/javascript">

   //  window.print(;

</script>

Example 2:

123consloe.log(123)   // 未捕获的语法错误:意外标识符
console.123)          // 未捕获的语法错误: 出乎意料的数字

When a syntax error occurs in JavaScript, only the code contained in the same thread as the syntax error will be affected, while the rest of the code in other threads will be execution, assuming that none of them depends on the code containing the error.

2. Runtime Errors

Runtime errors (also called exceptions) occur during JavaScript execution (compilation/ error after explaining).

1. ReferenceError (ReferenceError)

ReferenceError: An error occurs when referencing a variable that does not exist; assigning a value to an object that cannot be assigned, such as Errors that occur as a result of function execution or function assignment.

For example, the following situation:

//  引用了不存在的变量 
a()       // 未定义引用错误:a 未定义
console.log(b)     // 未引用引用错误:b未定义
//  给一个无法被赋值的对象赋值 
console.log("abc") = 1   // 未引用引用错误:左侧赋值无效

2. Range Error (RangeError)

RangeError: is an error that occurs when the valid range is exceeded. . There are mainly the following situations:

1) When the array length is a negative number, an error occurs;

2) When the method parameter of the Number object exceeds the range, an error occurs;

3) When the function stack exceeds the maximum value, an error occurs.

For example, the following situation:

// 数组长度为负数 
[].length = -5      // 范围错误:无效的数组长度
// Number对象的方法参数超出范围 
var num = new Number(12.34) 
console.log(num.toFixed(-1))   // 范围错误: 在Number.toFixed中,toFixEd()的数字参数必须在0到20之间。
// 说明: toFixed方法的作用是将数字四舍五入为指定小数位数的数字,参数是小数点后的位数,范围为0-20.

3. Type Error (TypeError)

TypeError: An error that occurs when a variable or parameter is not of the expected type.

Example: This error will be thrown when using primitive types such as new strings and Boolean values ​​and calling methods that do not exist in the object, because the parameter of the new command should be a constructor.

//  调用不存在的方法 
123()        // 类型错误: 123不是一个函数
var o = {} o.run()    // 类型错误: o.run 不是一个函数
// new关键字后接基本类型 
var p = new 456      // 类型错误: 456 不是构造函数

4. URIError: Wrong use of URL-related functions

URIError: Mainly because the parameters of URL-related functions are incorrect.

For example, the following situation:

decodeURI("%1")     // URIError: 在解码过程中变形了的URI

The error thrown when URI related parameters are incorrect, mainly involves the following six functions:

encodeURI, decodeURI(), encodeURIComponent( ), decodeURIComponent(), escape() and unescape()

5. EvalError: eval() function execution error

EvalError: In JavaScript below ES5, When the eval() function is not executed correctly, an evalError error is thrown.

For example, the following situation:

var myEval = eval; myEval("alert(&#39;call eval&#39;)");

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 .

3. Logical Errors

Logical errors may be the most difficult type of errors to track down. These errors are not the result of syntax or runtime errors. Rather, they occur when you make a mistake in the logic driving your script and you don't get the results you expect.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What are the types of errors that occur in JavaScript programming? Introduction to error types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more