Search code examples
javascriptfunctionthrowiife

Is throwing a function compliant with ES5/ES6 standard?


Is following code compliant with the standards (strict/sloppy)? It acts similar to an IIFE, but maybe readability has improved IMHO:

try {
    throw function (){}
} catch(foo) {
    foo();
}

I don't know about performances.


Solution

  • Yes. You can throw any value, including a function reference. What you throw doesn't have to be an Error (though that's best practice).

    You can see in the specification that throw accepts any Expression, which can result in any value, and catch faithfully provides the value thrown.