Is there something like "die" in JavaScript? I've tried with "break", but doesn't work :)
-
2see this: http://stackoverflow.com/questions/550574/how-to-terminate-the-script-in-javascript – stefita Sep 01 '09 at 09:20
-
"die", like "goto" are not complient with structured programming. These types of instructions should never be used for serious project. https://en.wikipedia.org/wiki/Structured_programming – Adrian Maire Apr 25 '13 at 14:10
-
3`function die(str) {throw new Error(str || "Script ended by death");}` Or something XD Plenty of better options out there, but this would work. Might be good for debugging, if you only want to run the first part of a script to make sure it works. – Niet the Dark Absol Oct 18 '13 at 19:37
-
@stefita Why is not your comment into the answers?? exit() was just what i need. The other way, is to run an autocall loop, will run 1599 times then stop the execution. Thx. – m3nda Feb 04 '15 at 19:15
-
@AdrianMaire Your heart's in the right place, but the "why" of a question shouldn't be at issue. The evils of `die`, `goto`, `eval`, etc. are endlessly regurgitated (and not without merit), but they all have their special uses, especially for low-level debugging. Otherwise languages wouldn't include them. In this case, the JS equivalents of `return` and `throw` are innocuous enough. – Beejor Jun 13 '15 at 16:35
-
@Beejor: I agree. In case of JS, I usually use 'alert' for debugging, which is like a die() for debugging purpose. But for the final code, I still think it's better to go through structured programming (or even some kind of OOP), even if it imply some more lines: the best code is no the one that work, but the one every programmer can understand easily. But you are right in all your statement. – Adrian Maire Jun 15 '15 at 05:55
-
I'm looking for something like that because I have some buggy code and find myself stuck in errors - thus, I can't read the logs in chrome devtools. I'd like something to kill V8 just to let me read these damn logs. There is [this function] (http://stackoverflow.com/questions/1361437/javascript-equivalent-of-php-s-die) but I don't think that's callable from inside the sandbox. – Loic Coenen Jun 29 '16 at 11:26
14 Answers
throw new Error("my error message");

- 11,455
- 13
- 33
- 46
-
8this is absolutely the answer and works just like die(); however one should not care for the red "1 Error" of firebug! – Alexar Dec 21 '10 at 23:52
-
3I think that if PHP has a "firebug" equivalent, it should also write "1 error" on die() ;-) Good answer! – Adrian Maire Apr 25 '13 at 14:04
-
1
-
-
This will not totally stop execution AFAIK, but only roughly around the throw. Specifics are very blurry but I'm pretty sure the script can keep running somewhere else. – Rolf Jan 29 '16 at 10:36
You can only break
a block scope if you label it. For example:
myBlock: {
var a = 0;
break myBlock;
a = 1; // this is never run
};
a === 0;
You cannot break a block scope from within a function in the scope. This means you can't do stuff like:
foo: { // this doesn't work
(function() {
break foo;
}());
}
You can do something similar though with functions:
function myFunction() {myFunction:{
// you can now use break myFunction; instead of return;
}}

- 35,104
- 14
- 75
- 93
-
6I never knew about labelling a block scope much less writing a block scope. Does it mean that `foo: {}` is an object? – enchance Jan 07 '12 at 18:40
-
4
-
Is there any alternative since you can't "break a block scope from within a function in the scope"? – bb216b3acfd8f72cbc8f899d4d6963 May 22 '19 at 22:31
You can simply use the return;
example
$(document).ready(function () {
alert(1);
return;
alert(2);
alert(3);
alert(4);
});
The return will return to the main caller function test1(); and continue from there to test3();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
return;
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
but if you just add throw ''; this will completely stop the execution without causing any errors.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
throw '';
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
This is tested with firefox and chrome. I don't know how this is handled by IE or Safari

- 8,335
- 21
- 84
- 109
-
2As far as I know, `return` exits only the enclosing function. It does not work when you want to stop executing the whole script. – André Leria Apr 18 '13 at 18:39
-
1hmmm yes you are right, it does not stop the execution of all the script. – themhz Apr 22 '13 at 09:26
-
`throw ""`: *...without causing any errors...* - Not quite... `Uncaught ""` – FZs Jul 02 '19 at 08:31
Just call die()
without ever defining it. Your script will crash. :)
When I do this, I usually call discombobulate()
instead, but the principle is the same.
(Actually, what this does is throw a ReferenceError
, making it roughly the same as spudly's answer - but it's shorter to type, for debugging purposes.)

- 13,404
- 6
- 46
- 58
-
The use of a custom undefined identifier is very creative! I like how `die` is intuitive and concise. It does lack the benefit of `throw` when it comes to logging a specific error message, but sometimes just the line number is enough. BTW, `die;` without the parentheses (un-)works too. – Beejor Jun 13 '15 at 16:49
-
-
If you're using nodejs, you can use
process.exit(<code>);

- 10,461
- 10
- 47
- 63

- 157
- 1
- 3
-
5If this would be about node.js, the question would have the tag [node.js](http://stackoverflow.com/questions/tagged/node.js) – FelixSFD Nov 19 '16 at 19:20
-
4@FelixSFD: Still it helped me, as I was searching for exactly this, completely disregarding the tags ;) – D. E. Jan 06 '17 at 13:42
It is possible to roll your own version of PHP's die:
function die(msg)
{
throw msg;
}
function test(arg1)
{
arg1 = arg1 || die("arg1 is missing");
}
test();

- 165
- 2
- 5
-
I wanted to mention that this won't work in cases like `var a = arguments[3] || die('message')`. instead, I think die should be: `function die(msg) { return eval(\`throw "${msg}"\`);)` but even then, I think this should just be hard placed on the line that breaks so that the error shows which line failed, eg `this.inputFile = argv[2] || eval('throw "this program requires an input file."');` – Dmytro Jul 10 '16 at 18:08
-
Probably, not sure eval() solves the problem if I'm understanding you. Ideally one would be looking at a stack trace if you were interested in where a failure was actually ocuring. Otherwise I would think it's safe to assume that you simply want to report some sort of basic failure message tor your user. – Kelmar Aug 24 '16 at 18:40
There is no function exit equivalent to php die() in JS, if you are not using any function then you can simply use return;
return;

- 389
- 4
- 10
use firebug and the glorious...
debugger;
and never let the debugger make any step forward. Cleaner than throwing a proper Error
, innit?

- 7,407
- 10
- 46
- 58
Global die() function for development purposes:
var die = function(msg) {
throw new Error(msg);
}
Use die():
die('Error message here');

- 654
- 7
- 15
There's no exact equaliant of language construct die
of PHP in Javascript. die
in PHP is pretty much equal to System.exit()
in Java, which terminates the current script and calls shutdown hooks.
As some users suggested; throw Error
can be used in some cases, however it never guarantees the termination of the current script.
There can be always an exception handling block surrounding your throw
statement- unless you call it on the top most level script block, which eventually exits only the script block you're executing.
However it won't prevent the second block from being executed here (prints hello):
<script type="text/javascript">
throw new Error('test');
</script>
<script type="text/javascript">
document.write("hello");
</script>

- 1,572
- 12
- 18
You can use return false; This will terminate your script.

- 2,547
- 2
- 26
- 42
-
3Only at the top level, presumably. PHP's die() can be called at any level and will cause the PHP interpreter to go away right there. – Rolf Jan 29 '16 at 10:33
-
1No, it will not. It will terminate/return the _current enclosing function_, and just exit that function and proceed. – Jun 28 '22 at 09:17
This should kind of work like die();
function die(msg = ''){
if(msg){
document.getElementsByTagName('html')[0].innerHTML = msg;
}else{
document.open();
document.write(msg);
document.close();
}
throw msg;
}

- 414
- 6
- 13
<script>
alert("i am ajith fan");
<?php die(); ?>
alert("i love boxing");
alert("i love MMA");
</script>

- 16,156
- 19
- 74
- 103

- 1
- 1