JavaScript equivalent of PHP’s die

Javascript

Javascript Problem Overview


Is there something like "die" in JavaScript? I've tried with "break", but doesn't work :)

Javascript Solutions


Solution 1 - Javascript

throw new Error("my error message");

Solution 2 - Javascript

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;
}}

Solution 3 - Javascript

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

Solution 4 - Javascript

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.)

Solution 5 - Javascript

If you're using nodejs, you can use

process.exit(<code>);

Solution 6 - Javascript

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();

JSFiddle Example

Solution 7 - Javascript

you can try with :

return;   

that work in case of stop process.

Solution 8 - Javascript

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;

Solution 9 - Javascript

use firebug and the glorious...

debugger;

and never let the debugger make any step forward. Cleaner than throwing a proper Error, innit?

Solution 10 - Javascript

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> 

Solution 11 - Javascript

Global die() function for development purposes:

var die = function(msg) {
	throw new Error(msg);
}

Use die():

die('Error message here');

Solution 12 - Javascript

You can use return false; This will terminate your script.

Solution 13 - Javascript

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;
}

Solution 14 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncupakobView Question on Stackoverflow
Solution 1 - JavascriptStephen SorensenView Answer on Stackoverflow
Solution 2 - JavascriptEli GreyView Answer on Stackoverflow
Solution 3 - JavascriptthemhzView Answer on Stackoverflow
Solution 4 - JavascriptBrilliandView Answer on Stackoverflow
Solution 5 - JavascriptrickbsguView Answer on Stackoverflow
Solution 6 - JavascriptKelmarView Answer on Stackoverflow
Solution 7 - JavascriptYassine CHABLIView Answer on Stackoverflow
Solution 8 - JavascriptManu JosephView Answer on Stackoverflow
Solution 9 - JavascriptnourdineView Answer on Stackoverflow
Solution 10 - JavascriptregulusView Answer on Stackoverflow
Solution 11 - JavascriptgummiostView Answer on Stackoverflow
Solution 12 - JavascriptBugfixerView Answer on Stackoverflow
Solution 13 - JavascriptMatas LesinskasView Answer on Stackoverflow
Solution 14 - JavascriptarimaView Answer on Stackoverflow