Javascript – How to terminate the script in JavaScript

dieexitjavascript

How can I exit the JavaScript script much like PHP's exit or die? I know it's not the best programming practice but I need to.

Best Solution

"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