iamrobin.
iamrobin.

Reputation: 1634

Return false in a function, when an if clause inside a loop breaks

I have a function, that runs throw an array of input values (for loop). Inside this loop I check with an if clause, if the validation returns true or false (calling an other function). When the validations returns false I want to break the for loop and return false in my function and jump out of it.

function getValues(){
  const values = [value1, value2, value3, value4, value5];
  for(var i=0; i<values.length; i++) {
    if(!validateForm(values[i]) {
      //break the for loop and jump out of getValues()
    }
  }
}

With an break statement in the if clause I can jump out of the for loop but can't ensure that only in this case the function will return false.

Upvotes: 1

Views: 2047

Answers (6)

Shashwat Gupta
Shashwat Gupta

Reputation: 5264

        function foo() {
            let list = [1, 2, 3, 4, 5]
            for (let i = 0; i <= list.length; i++) {
                if (list[i] == 3) {
                    // break the loop if you want break only with return statment
                    return false
                }
            }
        }
    
    
    
    console.log(foo(), 'foo');

//output 
    false foo

Upvotes: 0

P.S.
P.S.

Reputation: 16384

If you want to exit the function execution, you should use return, if you want to exit the loop and continue function execution - use break.

An example with return:

function loopFunc() {
  for (var i = 0; i <= 10; i++) {
    console.log("i:", i);
    if (i === 5) {
      return;
    }
  }
  console.log("This log will not be printed");
}

loopFunc();

An example with break:

function loopFunc() {
  for (var i = 0; i <= 10; i++) {
    console.log("i:", i);
    if (i === 5) {
      break;
    }
  }
  console.log("This log will be printed");
}

loopFunc();

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68393

You need to put return false in the if condition and return true after it.

function getValues(){
  const values = [value1, value2, value3, value4, value5];
  for(var i=0; i<values.length; i++) {
    if(!validateForm(values[i]) {
      return false; //notice this line
    }
  }
  return true ; //notice this line as well
}

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281726

You can return false when the validation return false, it will break out your loop and also return value from function

function getValues(){
  const values = [value1, value2, value3, value4, value5];
  for(var i=0; i<values.length; i++) {
    if(!validateForm(values[i]) {
      //break the for loop and jump out of getValues()
      return false;
    }
  }
  return true;
}

Upvotes: 1

Anne Douwe
Anne Douwe

Reputation: 691

You can just put return false there

function getValues(){
  const values = [value1, value2, value3, value4, value5];
  for(var i=0; i<values.length; i++) {
    if(!validateForm(values[i]) {
      // Break the loop and return false
      return false;
    }
  }
}

// For all other cases return true
return true;

Upvotes: 1

Ele
Ele

Reputation: 33726

Just return false;

function getValues(){
  const values = [value1, value2, value3, value4, value5];
  for(var i=0; i<values.length; i++) {
    if(!validateForm(values[i]) {
      //break the for loop and jump out of getValues()
       return false;
    }
  }

  return true;
}

Upvotes: 1

Related Questions