Abort function from within nested function

Abort function from within nested function
0.0 0


So we can have nested functions, right? And if we say return, it’s relative to scope: if I say return within a nested function it means the end of that function, but not of the larger one. But what if I’m inside a second-layer function and generate The Answer that ends the outer function?

Example: some of my thoughts so far on the Arguments Optional algorithm:

function addTogether() {
  var args = [...arguments];
    // if typeof element is not "number"--then that's the end of the whole ball game, right there!
    // return undefined--but not just "return" within the context of *this* function; time to "return" undefined for the whole addTogether function!
  if (args.length === 2) {
    return args[0] + args[1]; // That's the easy part. "If there are two arguments, add 'em."
  } else if (args.length === 1) {
    return function addEm(x) {
      return addTogether(args[0], x); // I know, this still needs work

In the args.every function, if I say return, I’m within the scope of that function, not the whole big overall function. I suppose I could just dodge the issue by using a for loop instead of .every, but I feel like I’ll be in this situation again and want to know. Is there a way to “return” the parent function from within a child function?


Thanks, yes, .some to check for typeof !== "number" makes more sense. And I realized that wrapping the whole line in an “if” solves my immediate problem–

  if (args.some(function(element){
    return typeof element !== "number";
  })) {
    return undefined;

–but my abstract question still stands. Is there any way to say “return” when you’re two layers into functions and have it return a value to the outer function, concluding it?


did you find the answer to your question?

I have wondered about that one many times and I always end up using some hacky solution like returning an object like return {x: false}; and then in the outer scope I use an if(x==false) {return}; Obviously a piece of sh*t :slight_smile:

Can you bring some light?



You can break the flow of the program by throwing an error in try catch clause but that’s a dirty hack. I’d suggest writing well designed programs using proper tools for the job instead.


So no, looks like not. Though looking at it now, a few days later, it looks to me as if I could name the second-layer function–say, function checkForNumber(element)–and call it, with instructions to return (outer scope) if the function itself returns something?


yeah, now that you mention it… maybe

`myFirstFunction () {               //this one is in the outer scope
     var mySecondFunction= function() { //inner scope
        do something;
        return something;         // we go back to the outer scope
if (mySecondFunction===whatever) {       //here we check the value returned by the inner function
    return;}               // we leave the outer function scope and go to global scope

it is similar to what I suggested in my first email, but much better… we declare a variable that is equal to the function, better, to the value returned by the function. A value that we can check to order a second return if we want.

Case solved?