Skip to content
Advertisement

JavaScript equivalent of PHP’s die

Is there something like “die” in JavaScript? I’ve tried with “break”, but doesn’t work 🙂

Advertisement

Answer

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;
}}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement