1

I'm developing a controller in angular. For some reason there is an If statement that is giving me a issue, yes an if statement.

The code is the following:

$scope.new = function(logoFile) {
if($scope.comprobarCampoDesc() && $scope.comprobarCampoFecha() && $scope.comprobarCampoName() ) 
{ 
//program logic
}

Also there is the other pieces of code:

$scope.comprobarCampoName = function(e) {
//program logic
return bol;
};


$scope.comprobarCampoDesc = function(e) {
//program logic
return bol;
};


$scope.comprobarCampoFecha = function(e) {
//program logic
return bol;
};

Ok, For any reason that I'm not able to identify, the if statement only checks 2 of 3 methods, depending of the order. In this concrete case it is ignoring "$scope.comprobarCampoName" but if I change the order other method is witch doesn't work.

Thanks for the help. Greetings.

3
  • Does it always check 2 of 3 methods or only if 2 of 3 methods are returning true ? Commented Nov 17, 2015 at 12:16
  • well you must know the && operator only evaluates the next operand( functions in you case ) only if the operand before evaluates to true or equivalent. Commented Nov 17, 2015 at 12:16
  • It's strange because all of the operands are false and allways execute 2 of them. Anyway thanks for the help. Finally I join the three methos in one in order to do only one call in the if statement. Commented Nov 18, 2015 at 10:18

1 Answer 1

2

In your case, if one of the conditions is equal to false, the if statement stops and does not iterate further.

If you really need to execute each of those, I recommend you to do the following :

var first = $scope.comprobarCampoDesc();
var second = $scope.comprobarCampoName();
var third = $scope.comprobarCampoFecha();

if (first && second && third){
   // execute
}
Sign up to request clarification or add additional context in comments.

1 Comment

The official term for this behavior, which is very common in programming languages, is Short-Circuit Evalution. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.