There is technically this block params proposal, which would allow programmers to receive a block as a parameter, that could even contain continue/break, and then the function receiving the block would be allowed to handle all of this. With a proposal such as this, the forEach function could be modified to work like this:
myArray.forEach {
if (something) {
continue;
} else {
break;
}
}
Unfortunately, this proposal has been pretty stagnant for a while, so I'm not sure if it's going to go anywhere.
Also, as @Jamesernator mentioned, we would basically just be making forEach behave more like a for-of loop. Might as well just use for-of instead. TBH, I never really use forEach anymore, as for-of is strictly more powerful in every way. The one exception is that forEach allows you to easily pass in a function defined elsewhere, like array.forEach(someFn)
, but in practice, I've never needed this.
As for break/continue in a ternary.
Break/continue, by nature, is a statement, not an expression. You're instructing the language on how to perform some control flow. It has no business being in an expression position. Ternary fits best when you need to evaluate and receive two potentially different values, depending on a condition, which isn't what's happening here. A normal if/then works better when you're trying to execute an imperative sequence of steps. This means, if you need to conditionally use break/continue, then use break/continue with a normal if, not with ternary, it's the better fit.
That being said, the do expression proposal does technically enable this behavior, since it enables any statement to be used in the expression position. The fact that break/continue could be used as an expression was seen more as an unfortunate necessary consequence to keep the proposal consistent, rather than something people should actually use. But, it is possible to do it like this:
const x = condition ? 2 : do { break; };
Though, it makes more sense to just write it like this:
if (!condition) {
break;
}
const x = 2;