In this tutorial, you will learn about the types of errors in JavaScript.

When you work in javascript programming language. If you write something wrong in script, So you must have seen that while working in it some errors come. There is some reason for this.

During you are writing a script, you get an error from writing some syntax wrong, or by writing some logic wrong. Or the executes the script and some error occurs.

JavaScript also has some similar errors. And each error comes for a different reason.

JavaScript error types

JavaScript has six types of errors that may raises during the script execution:

  •  EvalError
  •  RangeError
  •  ReferenceError
  •  SyntaxError
  •  TypeError
  •  URIError


JavaScript raise the EvalError when you use eval() function in the wrong manner. See the following example:

let e = new eval();


TypeError: eval is not a constructor

However, web browsers often throw the TypeError instead of EvalError in this situation.


The RangeError happens when a number is not in its range. See the following example:

try { let list = Array(Number.MAX_VALUE);
} catch (error) { console.log(; // "RangeError" console.log(error.message); // "Invalid array length"


Invalid array length

Inside the try block causes a RangeError because array length not defined here.


The ReferenceError raises when you reference a variable, a function, or an object that does not exist. See the following example:

try { var a = a + c;
} catch (error) { console.log(; // "ReferenceError" console.log(error.message); // "c is not defined"


c is not defined

In this example, the variable c does not exist therefore it causes a ReferenceError.


The SyntaxError raises in a string that you pass to the eval() function. See the following example:

try { eval('a x b');
} catch (error) { console.log(; // "SyntaxError" console.log(error.message); // "Unexpected identifier"


Unexpected identifier

Outside the eval() function, JavaScript stops executing the code whenever it finds a SyntaxError.


The TypeError happens if a variable is of an unexpected type or access to a nonexistent method.

try { let x = new "String"; } catch(error) { console.log(; // "TypeError" console.log(error.message); // "String" is not a constructor"


TypeError "String" is not a constructor

In this example, we tried to create a new instance of a literal string which caused a TypeError error.

let db = { host: 'localhost', port: 3306, user: 'root', password: 'secret'
}; try { db.connect();
} catch (error) { console.log(; // "TypeError" console.log(error.message); // "db.connect is not a function"

In this example, we tried to access the connect() method of the db object, which does not exist, therefore a TypeError error occurred.


The URIError error raises when using the encodeURI() or decodeURI() with a malformed URI. See the following example:


Throwing errors

To throw an error, you can use the throw js operator. See the following example:

throw 'ABC';
throw 123;

Whenever the JavaScript reaches the throw operator, it halts the execution of the script immediately. In that case, to continue the script execution, you need to use the try...catch statement to catch the value that was thrown. See the following example:

try { throw 123;
} catch (error) { console.log(error); // 123
console.log('continue!'); // "continue!"

You can also use the typeError mentioned earlier as an error to throw. See the following example:

function add(a, b) { if (typeof a !== 'number') { throw TypeError('The first argument must be a number'); } if (typeof b !== 'number') { throw TypeError('The second argument must be a number'); } return a + b;
} add('string', 1);


TypeError: The first argument must be a number

Custom Error

You can create a custom error that obtains from a javascript built-in error. See the following:

function InvalidCallError(message) { = 'InvalidCallError'; this.message = message;
} InvalidCallError.prototype = Object.create(Error.prototype);
InvalidCallError.prototype.constructor = Error;

Then, you can throw the custom error as shown in this example:

throw new InvalidCallError('Invalid function call');


"Uncaught InvalidCallError: Invalid function call"

Related Posts