Best practice for exiting Javascript completely

For debugging purposes, it is sometimes useful to insert a line of code to easily stop everything from running, and to be able to do this from inside a function or outside all functions.

PHP’s exit() does this. But I’m not aware of any similar method in Javascript. I have read thow''; works, but I wonder if there is a more elegant way of doing it.

In server side code, you can use process.exit(). There is no way to exit the interpreter in the browser; throwing an exception is as close as you can come to that, though it will have no effect on the event loop.

1 Like

You can call debugger, which by default sets a breakpoint in your code.

From there you can use the dev tools of your browser to inspect your code :slight_smile:

1 Like