0

I understand return basically terminates a function. But somehow in this case, I'm not sure which return is actually finishing up a function.

var THRESHOLD = 12;
var v = [5, 2, 16, 4, 3, 18, 20];
var res;

res = v.some(function(element, index, array) {
  console.log('element:', element);
  if (element >= THRESHOLD) {
    return true; //#1
  }

  return false; // #2
});
console.log('res:', res);

Say it's iterating at v[0]=5, it skips if(){} and go ahead to return false //2, Why is that after #2 return, the function still keep looping?

2
  • sorry about multiple editting, it was really confusing expresion Commented May 4, 2015 at 17:58
  • oh sorry my bad.. i confused every with some..... Commented May 4, 2015 at 18:00

2 Answers 2

5

The function does not keep running. It is being called several times because you are iterating over the array

Sign up to request clarification or add additional context in comments.

2 Comments

but doesn't the return false stop the iteration?
@XiaoQu No, otherwise how could it find your true value? From MDN: some() executes the callback function once for each element present in the array until it finds one where callback returns a true value. Returning true will stop the iteration though
0

that happends because the 1st element is less than 10 and that 1st return false kill the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.