Signup/Sign In

JavaScript Errors

JavaScript Errors are used by JavaScript to inform developers about various issue in the script being executed. These issues can be syntax error where the developer/programmer has used the worng syntax, it can be due to some wrong user input or some other problem.

JavaScript reports these issues in form of Errors which can be seen in the browser console, when you run your JavaScript code.

For example, if I run the below JavaScript code,

/* 
    try to use variables a and b,
    without defining them
*/
let result = a + b;


Uncaught ReferenceError: a is not defined

As you can see in the output, JavaScript threw a ReferenceError, which means that no reference for variable a was found. Because, to use any JavaScript variable we should always declare and define it first.

JavaScript Error Types

Now that we know what Errors are and they can occur for many different reasons. Let's see what are the different types of Errors in JavaScript along with a few examples.

Whenever a runtime error occurs, JavaScript creates an Error object and throw it. The Error object is the base object on which other specific Error types are based. We can also define our custom user-defined Error types in JavaScript.

JavaScript Error Object has two fields:

Property Description
name Used to set or get the name of Error
message Used to set or get the message of Error

Following are the 6 types of Error objects that JavaScript can throw based on the type of the error:

Error Object Description
EvalError JavaScript will throw this error when any error will occur in the eval() function.
RangeError JavaScript will throw this error when for a number or any other parameter "out of range" occurs.
ReferenceError JavaScript will throw this error when you try to use some variable or object which is not declared or defined.
SyntaxError JavaScript will throw this error when there is some issue with the syntax of the JavaScript code.
TypeError JavaScript will throw this error when there is a type mismatch in your code. If JavaScript is expecting a value of some other type and in code it gets a different type.
URIError JavaScript will throw this error when any error occurs in encodeURI() or decodeURI() functions.

JavaScript Throws Error

Whenever an error occurs during JavaScript code execution, the code execution stops and JavaScript prints an error in the console. This is also called as "throwing an error". This is a standard term which is used in all the programming language.

JavaScript internally captures the Error and then throw it which breaks the code execution.

If you want to do the same thing in your code yourself where you want to break the code execution and throw error, you can do so using the throw statement.

JavaScript throw statement

We can use the throw statement in our JavaScript code to throw a custom error/exception where we can provide a message while throwing the error. This message can be a string message, a number or any boolean type(true or false). Let's take an example to see this:

throw "Some error message";
throw 404;
throw false;


Uncaught Some error message

As the code execution breaks when a throw statement is encountered, hence only the first throw statement will get executed. To handle errors which are thrown by JavaScript or using the throw statement, we have the try and catch blocks, which we will cover in the next tutorial.

JavaScript Custom Error (User-defined Error):

In JavaScript we can also define our custom Error object. Here we have discussed the ECMAScript2015 way which is the prototypal way of defining a user-defined error type.

When you will run the above code example, you will get an error message in your browser's console. To open your browser's developer's tool, press F12 in windows and Command + Option + I in Mac OSX. Following is the output:


MyError {name: "MyError", message: "This is a custom error", stack: "Error at new MyError (<anonymous>:6:31)

In ECMAScript2016 changes were made where in we can define custom Error classes, which we will cover in later tutorial.

So in this tutorial we got a basic introduction to JavaScript Errors, its various different types, how to throw an error using the throw statement and how to write a custom Error type. In the next tutorial we will learn how to handle Errors and Exception in JavaScript.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight