DEV Community

Cover image for Understanding Errors In JavaScript: Error Objects
Obaseki Noruwa
Obaseki Noruwa

Posted on

Understanding Errors In JavaScript: Error Objects

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

Image description

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

Image description

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

Image description

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

Image description

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

Image description

Image description

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

Image description

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.

Top comments (0)