Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript EvalError Object

JavaScript EvalError Object

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);
};


Output

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));


Output

Error occurred
undefined

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments