简体   繁体   中英

Javascript return function meaning

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?

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

发生这种情况是因为第一个元素小于10,并且第一个返回false杀死了循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM