What Are the Different Types of JavaScript Errors? — and How to Handle Them
We see errors everywhere, from the browser console to the terminal on our computer running Node.js.
This article focuses on This describes the types of errors we might encounter during JS development.
Tip: Correct errors make the difference between a quick, painless experience and slow, painless development . infernal. . When writing reusable code, make sure you write clear and understandable errors.
Related: Common Errors That People Make While Programming In Python
The following 7 types of errors in JavaScript are:
Syntax Error: The error occurs when a predefined syntax is used incorrectly. wrong.
const func = () =>
console.log(hello)
}
Output:
}
^
SyntaxError: Unexpected token }
In the previous example, the code that calls the d constructor is missing an open parentheses syntax error.
Reference error: If a reference to the variable is not found or is missing, a reference error will be reported.
console.log(x);
Output:
console.log(x);
^
ReferenceError: x is not defined
Internal error: In the JS engine, this error occurs more often when there is too much data and the stack exceeds its limits critical dimensional capacity. If there are too many recursion patterns, switching cases, etc., the JS engine will become saturated.
switch(condition) {
case 1:
...
break
case 2:
...
break
case 3:
...
break
case 4:
...
break
case 5:
...
break
case 6:
...
break
case 7:
...
break
... up to 500 cases
}
Output: The result will look like Internal Error.
URI Error: If incorrect characters are used in a URI function, an error will be returned.
console.log(decodeURI("https://www.example.com/testpage"))
console.log(decodeURI("%sdfk"));
Output:
console.log(decodeURI("%sdfk"));
^
URIError: URI malformed
RangeError: If a range of expected values is requested, an error will occur as shown below:
const checkRange = (num)=>{
if (num < 30) throw new RangeError("Wrong number");
return true
}
checkRange(20);
Output:
if (num < 30) throw new RangeError("Wrong number");
^
RangeError: Wrong number
Evaluation error: Current JavaScript engines and EcmaScript specifications do not trigger this error. However, for backward compatibility reasons, it is still available. The error is called when using the inverse eval() function, as shown in the following code block:
try{
throw new EvalError("'Throws an error'")
}catch(error){
console.log(error.name, error.message)
}
Output:
EvalError 'Throws an error'
Type error: An error occurs if the value is used outside the scope of its data type.
let num = 15;
console.log(num.split("")); //converts a number to an array
Output:
console.log(num.split("")); //converts a number to an array
^
TypeError: num.split is not a function
What are Errors in JavaScript?
The JavaScript code may encounter various errors when competition. Errors can be caused by programming errors, incorrect input, or other unpredictable events.
Programming errors can be divided into two types:
Program error: — In this case, the program may need to handle this error through its error handlers. An example would be a network outage, timeout error, etc.
Developer Error: — The programmer caused an error. It could be a syntax error, a logical error, a semantic error, etc.
Read More: Etsy 429 Error And Solution: Etsy 429 Too Many Requests Error
Read Also: Why Is James Dooley The Godfather Of Seo Marketing?
Don’t Miss: My Dubai Travel Blog