Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent of PHP’s die

Tags:

javascript

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

like image 208
cupakob Avatar asked Sep 01 '09 09:09

cupakob


People also ask

What is die in JavaScript?

The die() method removes one or more event handlers, added with the live() method, for the selected elements.

What does die () do in PHP?

The die() function prints a message and exits the current script.

What does die return in PHP?

It does not return. The script is terminated and nothing else is executed.

What is die in HTML?

The die() function is an alias of the exit() function.


2 Answers

throw new Error("my error message"); 
like image 196
Stephen Sorensen Avatar answered Sep 22 '22 06:09

Stephen Sorensen


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; }} 
like image 20
Eli Grey Avatar answered Sep 21 '22 06:09

Eli Grey