Understanding Errors In JavaScript: Error Objects
Tags: javascript, beginners, tutorial, programming
Published: 2023-03-22 11:13:18

When executing JavaScript code, different errors can occur, understanding errors in javascript cannot be overstated. Errors can occur due to wrong input, and other unforeseeable things. Error objects are in-built in javascript to enable coders/programmers know and understand their errors for easier debugging where necessary. 

**What are Error Objects**
An error object provides error information when an error occurs. Error object has a collection of properties: name, message, file name, line number. These properties helps us have a better understanding of the problem.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfw0q7qtnwditbacc06r.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdvag7a2l6jsbrou163m.PNG)


In the image above, SyntaxError is the error name, Invalid or unexpected token is the error description. At the right hand side we have the file name and line number.

Understanding error object properties helps you understand the problem faster for easier debugging. Below are some errors you will likely run into while executing javascript code.



**EvalError**: This occurs when the eval() inbuilt function is used in a way that is not allowed.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cykon7qtgsq1sj88pj72.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lviu1grnb53zpid1cam6.PNG)


An Error object; EvalError is thrown in the try block. The error is caught in the catch block and its stack trace is logged to the console using our ultimate debugger method (console.log). The output is produced with the name, description, file name and line number.



**ReferenceError**: This occurs when referencing a variable that does not exist or has not yet been initialized in the current scope.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6qmw401ggsk3wd5hjg2.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmafx3fmvboys5dojbch.PNG)


In the above example, the variable fulname does not exist. Fixing an error like this, is to cross check the spellings and analyze the code.



**TypeError**: This error occurs when a variable or parameter is not of a valid type or is not of the expected data type.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gn7c1hz6q7nd34qreyfh.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuih5et3zbu5sllus2p9.PNG)



**RangeError**: An RangeError is thrown when a numeric variable or parameter is outside its valid range.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czpm8oeeftak9wcshb7y.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bdj6evwymjhk3ry0wnt.PNG)



**URIError**: A URIError error object is thrown if you use illegal characters in a URI function, The error occurs due to the use of an invalid character in encoding the url.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvxmrp29lucak8vetavc.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gir1kctl7xdfgmxz44lp.PNG)



**Conclusion**
When you understand the concepts of error objects, debugging and fixing your error will be simple and this will spare you from spending hours online looking for solutions.