javascriptexit

Is 'exit' a universally supported way of stopping execution in JavaScript?


I'm new to JavaScript, and found by accident that the following code works as I want:

alert("This shows");
exit;
alert("This doesn't show");

I want to early terminate all subsequent JavaScript on the current page. This works in the browsers I've tested it in. But I can't find any documentation that this is a legitimate keyword in JavaScript, and as such I'm worried that some browsers might not support it. All other suggestions mention 'return', but I'm not returning from a function, simply stopping the proceeding sequential JavaScript execution.

Is there any specific documentation for exit;?


Solution

  • There is no "exit" in JavaScript. All you did was introduce an error on the page.

    Looking at the console you would see Uncaught ReferenceError: exit is not defined

    When would it not work?

    What should you do?