0% found this document useful (0 votes)
8 views

Javascript Errors

Uploaded by

mubashirqazi2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Javascript Errors

Uploaded by

mubashirqazi2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

All Types of Errors in JavaScript

1. Syntax Errors:
- These occur when the JavaScript parser encounters syntax that does not conform to the

language rules.

- Example: Missing closing brackets, or using undeclared variables.

- Example Code:

console.log("Hello World!;

2. Reference Errors:
- Occur when a non-existent variable is referenced.

- Example: Trying to access a variable that has not been defined.

- Example Code:

console.log(myVariable); // Uncaught ReferenceError

3. Type Errors:
- Occur when a variable or parameter is not of a valid type.

- Example: Invoking a non-function as a function.

- Example Code:

let x = 5;

x(); // Uncaught TypeError

4. Range Errors:
- Occur when a value is not in the set or range of allowed values.

- Example: Trying to create an array with an invalid length.

- Example Code:

let arr = new Array(-1); // Uncaught RangeError

5. Eval Errors (Legacy):


- Occur with the misuse of the eval() function. These are less common in modern JavaScript as

eval is discouraged.
- Example: Using eval incorrectly.

6. URI Errors:
- Occur when there is an issue with the use of URI handling functions (encodeURI, decodeURI).

- Example: Passing malformed URI sequences.

- Example Code:

decodeURI('%'); // Uncaught URIError

7. Logical Errors:
- Occur due to flaws in the program logic.

- Example: Writing code that runs but does not produce the expected output.

- Example Code:

let total = 10 - 5; // Expecting 15 but logically incorrect.

8. Aggregate Errors (ES2021):


- Used with Promise.any() or other cases involving multiple errors.

- Example: Handling multiple errors collectively.

- Example Code:

let error = new AggregateError([

new Error("First Error"),

new Error("Second Error")

], "Multiple errors occurred");

9. Custom Errors:
- Developers can define their own error types for specific cases.

- Example Code:

class CustomError extends Error {

constructor(message) {

super(message);

this.name = "CustomError";

}
throw new CustomError("This is a custom error!");

Error Handling in JavaScript:

To handle these errors, JavaScript provides the try-catch-finally construct:

Example:

try {

// Code that may throw an error

let num = x / 0;

} catch (error) {

console.error("An error occurred:", error.message);

} finally {

console.log("This will run regardless of an error.");

You might also like