;

Types of Error in Javascript


Tutorialsrack 12/06/2021 Jquery Javascript

In this article, you’ll learn about the types of errors in javascript. When you work in a javascript programming language. If you write something wrong in the script, you must have seen that while working on it some errors come. There are some reasons for this. While you are writing a script, you get an error from writing some wrong syntax, or by writing some wrong logic. Or when you execute the script and some error occurs.

JavaScript also has some similar errors and each error occurs for a different reason.

Types of Error in Javascript

JavaScript has six types of errors that may occur during the execution of the script:

  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError

Error Name

Description

EvalError

An error has occurred in the eval() function

RangeError

A number “out of range” has occurred

ReferenceError

An illegal reference has occurred

SyntaxError

A syntax error has occurred

TypeError

A type error has occurred

URIError

An error in encodeURI() or decodeURI() has occurred

1. EvalError 

This error indicates that an error occurs in the global eval() function i.e. JavaScript raises the EvalError when you use the eval() function in the wrong way. Newer versions of JavaScript do not throw this error anymore, instead of this, relied on syntax error.

Example: EvalError
let e = new eval(); 
Error

TypeError: eval is not a constructor

However, browsers often throw the TypeError instead of EvalError in this case.

2. RangeError

This error indicates that when a number is not in range.

Example: RangeError
try {
    let list = Array(Number.MAX_VALUE);
} catch (error) {
    console.log(error.name); // "RangeError"
    console.log(error.message); // "Invalid array length"
}
Error

"RangeError"

"Invalid array length"

3. ReferenceError

This error occurs when we want to refer to some variable, function, or object that doesn’t exist (an invalid reference is used) i.e. that variable hasn’t been declared. It is one of the most common and frequently occurring errors.

Example: ReferenceError
try {
    var a = a + z;
} catch (error) {
    console.log(error.name); // "ReferenceError"
    console.log(error.message); // "z is not defined"
}
Error

"ReferenceError"

"z is not defined"

4.  SyntaxError

This error occurs when the syntax of the script is invalid i.e. a syntactical incorrect statement is present in the javascript code. As for syntax errors, an interpreted language like JavaScript won’t throw those kinds of errors until the script is loaded into and read by the web browser.

Example: SyntaxError
try {
    eval('a x b');
} catch (error) {
    console.log(error.name); // "SyntaxError"
    console.log(error.message); // "Unexpected identifier"
}
Error

"SyntaxError"

"Unexpected identifier"

5. TypeError

This error occurs when a variable is of an unexpected type or access to a nonexistent method.

Example: TypeError
try {
  let x = new "String";  
} catch(error) {
  console.log(error.name); // "TypeError"
  console.log(error.message); // "String" is not a constructor"
}
Error

"TypeError"

"\"String\" is not a constructor"

6. URIError

This error occurs when the use illegal characters that are invalid in URI-related methods/functions (encodeURI() or decodeURI()).

Example: URIError
console.log(decodeURIComponent('%E0%B4%A')); 
Output

"error"

"URIError: URI malformed

I hope this article will help you to understand the types of errors in javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags