# Different types of native errors in JavaScript

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.

```javascript
function myFunc() }
console.log('hey');
// Opening curly brace is missing {
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690011349976/fe934fb2-cbf9-4913-b837-05cb34445df8.png align="center")

### 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.

```javascript
console.log(userId);
// userId is not defined to console
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690013477686/5c98ef83-eb40-4989-a3c3-34d4d18624e0.png align="center")

### 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.

```javascript
let userId = 982025;

//Converting a number to an array will throw an error
console.log(userId.split(""));
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690017998465/456477e3-09ff-4704-bc65-beaeb659fa26.png align="center")

### 4\. Range Error

[`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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

```javascript
const checkUser = (userId)=> {
  if (userId) throw new RangeError("User id exist!");
  return false;
}

checkUser(982025);
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690018182215/83bb92ea-5f95-4509-8764-a4668dc2e776.png align="center")

### 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.

```javascript
try{
  throw new EvalError("'evaluation' error")
} catch(error){
  console.log(error.name, ':', error.message)
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690018268908/f90f16b3-95b5-41af-bdc1-e9620b1a71c0.png align="center")

### **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:

```javascript
console.log(decodeURI("https://jsonplaceholder.typicode.com/todos"))

console.log(decodeURI("%abcd"));  // Wrong URI, URIError thrown
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690018360107/a3bcf4aa-9b8b-469e-9fa6-46deca714c35.png align="center")

### 7\. Internal Error

The [`InternalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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
    

```javascript
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**](https://twitter.com/akhtarvahid) **|** [**Blog**](https://medium.com/@akhtarvahid) **|** [**LinkedIn**](https://www.linkedin.com/in/akhtarvahid/) **|** [**Github**](https://github.com/akhtarvahid)

---

### Links and Resources

* [**Deep-copying objects in JavaScript**](https://akhtarvahid.hashnode.dev/deep-copying-objects-in-javascript)
    
* [**Flatten nested array in javascript**](https://akhtarvahid.hashnode.dev/flatten-nested-array)
    
* [**CRUD Application in React.js with Redux and Hooks**](https://akhtarvahid.medium.com/crud-application-in-react-js-with-redux-and-hooks-e0367b6aeeb2).
    
* [**Create a simple quiz application using react js.**](https://github.com/akhtarvahid/quiz-app)
    
* [**How to deploy a React app from GitHub to Netlify**](https://akhtarvahid.medium.com/how-to-deploy-a-react-app-from-github-to-netlify-8c7364653e4c)
    
* [**How to access/fetch the local JSON file to React.**](https://akhtarvahid.medium.com/how-to-access-fetch-the-local-json-file-to-react-5ce07c43731d)
