admin管理员组

文章数量:1278880

Here is my test:

var test = function () {
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return true;
}​;

alert(test());

I would like the test function to return false but it returns true.
Why? How should I fix the code? Please see the ments for more details.

Here is my test:

var test = function () {
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return true;
}​;

alert(test());

I would like the test function to return false but it returns true.
Why? How should I fix the code? Please see the ments for more details.

Share Improve this question edited Oct 16, 2012 at 15:39 simon 12.9k27 gold badges82 silver badges115 bronze badges asked Oct 11, 2012 at 21:51 Lorraine BernardLorraine Bernard 13.4k23 gold badges85 silver badges138 bronze badges 1
  • If you have to iterate a set of jQuery objects you can make it easy. You can find the first element that match a given predicate using .filter(...).first() – Adriano Repetti Commented Oct 11, 2012 at 21:58
Add a ment  | 

5 Answers 5

Reset to default 13

Returning false from the .each() callback just halts the .each() iteration. It doesn't return from the enclosing function; the only way to do that in JavaScript is to throw an exception.

What you could do is set a flag:

var test = function () {
    var abort = false;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            abort = true;
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return !abort;
}​;

It returns true because the inner return false returns the anonymous function, which only instructs jQuery to end the $.each loop early.

Use a variable outside the inner function to properly juggle the return status.

var test = function () {
    var retVal = true;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            retVal = false;
            return false;
        }
    });
    return retVal;
}​;

You could also change your code to not use the $.each method, if a simply for...in loop would suffice:

var test = function () {
    var retVal = true;
    for (var value in [1, 2]) {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            return false;
        }
    };
    return true;
};

That is because return false; just breaks out of the $.each loop.. but not the function.

It returns true from the last statement

fix it by changing your code to:

var test = function () {
    var returnValue = true;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            returnValue = false;
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return returnValue;
}​;

You have two function definitions:

  • Inner Function (in $.each): returns false
  • Outer Function (window.test): returns true

Capture when exited:

var arr = [1,2,3,4,5,6,7,8,9];
var breakpoint = undefined;
$.each(arr, function(i,val){
   if (val==4){  // some break condition
      breakpoint = {index:i,val:val};
      return false;
   }
   return true;
});

console.log('break point:', breakpoint); // breakpoint.index=3, breakpoint.val=4

Then in your outer function you can do something like return typeof breakpoint !== 'undefined';, or set a returnValue as others have suggested.

本文标签: javascriptWhy does this function return true instead of falseStack Overflow