This article gives you a detailed introduction to the six error types in JavaScript. Students who have just entered the front-end pit and are not very good at English are still scratching their heads over console errors? Today I will take you to take a look at the common error types in JavaScript. If you are not familiar with JavaScript errors, you can take a look at this article.
The console error information in js is mainly divided into two categories. The first One type is grammar error. If this type of error is encountered during the pre-parsing process, it will cause the entire js file to fail to execute. 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 =
2. Uncaught ReferenceError: Reference error
An error 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
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.
4. TypeErrorType error
An error that occurs when a variable or parameter is not of the expected type. For example, using newString, Boolean and other primitive types 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
5. URIError, URL error
is mainly caused by incorrect parameters of related functions.
decodeURI("%") // Uncaught URIError: URI malformed at decodeURI
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')");
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]]]),
The first parameter represents the error message, the second is the file name, and the third is the line number.
Summary
The above are the six error types in JavaScript introduced by the editor. I hope it will be helpful to everyone! !
Related recommendations:
JavaScript to implement quick sort analysis
JavaScript experience asynchronous better solution sharing
javascript matches the regular expression code commented in js
The above is the detailed content of Detailed explanation of six error types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment