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.
JavaScript has six types of errors that may occur during the execution of the script:
Error Name |
Description |
EvalError |
An error has occurred in the |
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 |
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.
let e = new eval();
TypeError: eval is not a constructor
However, browsers often throw the TypeError
instead of EvalError
in this case.
This error indicates that when a number is not in range.
try {
let list = Array(Number.MAX_VALUE);
} catch (error) {
console.log(error.name); // "RangeError"
console.log(error.message); // "Invalid array length"
}
"RangeError"
"Invalid array length"
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.
try {
var a = a + z;
} catch (error) {
console.log(error.name); // "ReferenceError"
console.log(error.message); // "z is not defined"
}
"ReferenceError"
"z is not defined"
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.
try {
eval('a x b');
} catch (error) {
console.log(error.name); // "SyntaxError"
console.log(error.message); // "Unexpected identifier"
}
"SyntaxError"
"Unexpected identifier"
This error occurs when a variable is of an unexpected type or access to a nonexistent method.
try {
let x = new "String";
} catch(error) {
console.log(error.name); // "TypeError"
console.log(error.message); // "String" is not a constructor"
}
"TypeError"
"\"String\" is not a constructor"
This error occurs when the use illegal characters that are invalid in URI-related methods/functions (encodeURI()
or decodeURI()
).
console.log(decodeURIComponent('%E0%B4%A'));
"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!
Comments