How to terminate the script in JavaScript?

“exit” functions usually quit the program or script along with an error message as paramete. For example die(…) in php

die("sorry my fault, didn't mean to but now I am in byte nirvana")

The equivalent in JS is to signal an error with the throw keyword like this:

throw new Error();

You can easily test this:

var m = 100;
throw '';
var x = 100;

x
>>>undefined
m
>>>100

Leave a Comment