Different types of native errors in JavaScript

Software Engineer @YaraInternational | Full-stack developer | Problem solver |...
In every programming language, errors occur. We will focus on the types of errors that we may encounter during the development of something in Javascript.
1. Syntax error
SyntaxError - This is done when you use an already pre-defined syntax in the wrong way.
If you run the below code. It will throw an error SyntaxError because there is no opening brace } for the function.
function myFunc() }
console.log('hey');
// Opening curly brace is missing {

2. Reference Error
ReferenceError - This occurs when a variable reference can’t be found or is not declared.
Run the code below to see the type of error it logs in the console.
console.log(userId);
// userId is not defined to console

3. Type Error
TypeError() - This is an error caused by misusing a value outside its own data type scope. Take a look at the below example along with the console error message.
let userId = 982025;
//Converting a number to an array will throw an error
console.log(userId.split(""));

4. Range Error
RangeError is thrown when trying to pass a value as an argument to a function that does not allow a range that includes the value
const checkUser = (userId)=> {
if (userId) throw new RangeError("User id exist!");
return false;
}
checkUser(982025);

5. Evaluation Error
EvalError() - This is no longer thrown by the current JavaScript Engine or EcmaScript Specification. However, it still exists for backward compatibility. It is called when you use the eval()backward function, as shown below.
try{
throw new EvalError("'evaluation' error")
} catch(error){
console.log(error.name, ':', error.message)
}

6. URI Error
URIError() - This is called whenever a wrong character(s) is used in any URI function. A sample of this is shown below:
console.log(decodeURI("https://jsonplaceholder.typicode.com/todos"))
console.log(decodeURI("%abcd")); // Wrong URI, URIError thrown

7. Internal Error
The InternalError object indicates an error that occurred internally in the JavaScript engine. when it has too much data to handle and the stack grows way over its critical limit.
Example cases are mostly when something is too large, e.g.
Too many switch cases
Too many parentheses in regular expression
Array initializer too large
Too much recursion
switch(type) {
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
}
Errors in programming can be divided into two types. These are
Program Error:
This is an error encountered by the program that might require error handlers to manage. An example could be timeout errors, network disconnection, HTTP response errors, etc.Developer Error:
This is an error caused by the programmer. Such as syntax errors, logical errors, semantic errors, etc.
Conclusion
In JavaScript, errors can occur. To beat it, we need to know the types of native errors we can throw.
So, whenever an error is thrown either in your terminal or browser, you can now easily spot where and how the error occurred, with it you can write a better, less error-prone code.
Thanks for reading. I hope this will be helpful for you.
Say Hello! Twitter | Blog | LinkedIn | Github

