There are several types of errors that we encounter in JavaScript, for example, SyntaxError, RangeError, ReferenceError, EvalError, etc. The EvalError indicates an error regarding the global eval() function.
Newer versions of JavaScript however do not throw EvalError.
Syntax:
new EvalError()
new EvalError(message)
Parameters: The message is an optional parameter that provides details about the exception that occurred.
Return value: A newly constructed EvalError object.
Below are some examples of JavaScript EvalError.
Example 1: In this example, The code throws an EvalError, catches it, and logs whether the error is an instance of EvalError, its message, and its name.
Javascript
try { throw new EvalError( 'EvalError has occurred' ); } catch (e) { console.log(e instanceof EvalError); console.log(e.message); console.log(e.name); }; |
true EvalError has occurred EvalError
Example 2: In this example, The code checks if the score is less than 0, throws an EvalError with a custom message, and logs the error message when score is -3.
Javascript
let score = { checkerror: function (score) { if (score < 0) { try { throw new EvalError( 'Error occurred' ); } catch (e) { console.log(e.message); } } } } console.log(score.checkerror(-3)); |
Error occurred undefined