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:
JavaScript
x
7
1
myBlock: {
2
var a = 0;
3
break myBlock;
4
a = 1; // this is never run
5
};
6
a === 0;
7
You cannot break a block scope from within a function in the scope. This means you can’t do stuff like:
JavaScript
1
6
1
foo: { // this doesn't work
2
(function() {
3
break foo;
4
}());
5
}
6
You can do something similar though with functions:
JavaScript
1
4
1
function myFunction() {myFunction:{
2
// you can now use break myFunction; instead of return;
3
}}
4