&& and execution when the boolean function returns false

I do not understand javascript behavior.

I am doing a jquery ui dialog form validation. This seems to be a javascript issue, not a jQuery issue.

To check, I execute a function for fields that return true or false, and a boolean variable gets the result of the sequential && & operator. Like this:

bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." ); bValid = bValid && checkLength(libelle, "Libell\351", 1, 100 ); bvalid = bValid && checkLength(description, "Description", 1, 250); 

Here, for information, check functions:

 function checkLength( o, n, min, max ) { if ( o.val().length > max || o.val().length < min ) { o.addClass( "ui-state-error" ); if(o.val().length == 0) { textError = textError + "le champ " + n + " est requis !\n"; } else { textError = textError + "Taille de " + n + " entre " + min + " et " + max + "caract\350res.\n"; } return false; } else { return true; } } function checkRegexp( o, regexp, n ) { if (!(regexp.test(o.val()))) { o.addClass( "ui-state-error" ); textError = textError + n + "\n"; return false; } else { return true; } } 

It is expected that all functions will be executed, and all invalid fields will be marked incorrectly when concatenating error messages. For information, the bValid variable contains the logical result of sequential && operators. This last point works; no problems.

The real behavior is that when a function returns false , the following functions do not seem to be executed. As a result, only the first incorrect field is encountered.

Why?

+4
source share
5 answers

Because the && operator is shorted. This means that since the compiler / interpreter / is independent of the fact that both operands of both sides for the && operator must be true, if the first one is false, the second does not execute.

If you want your function to execute exactly, just replace the order of the operands with && :

 bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." ); bValid = checkLength(libelle, "Libell\351", 1, 100 ) && bValid; bvalid = checkLength(description, "Description", 1, 250) && bValid 
+6
source

JavaScript uses a short circuit rating . that is false, and something is always false, so why bother with 2nd computation?

+4
source

Because Javascript will simplify and "optimize" ... if the first operand of the && double operation is already false, the result will probably be false, so it won’t do the second part.

+1
source

He called a short circuit. && can be logically considered the “and-all” syntax when the second expression is not evaluated if the first expression is not executed. If you want the second expression to be processed independently of the first, you can consider changing the order or trying:

 bValid = bValid & checkLength(description, "Description", 1, 250); 

Although it is functionally equivalent

 bValid = checkLength(description, "Description", 1, 250); 
+1
source

Allow this with the actual output:

 bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." ); bValid = bValid && checkLength(libelle, "Libell\351", 1, 100 ); bvalid = bValid && checkLength(description, "Description", 1, 250); false = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." ); bValid = false && checkLength(libelle, "Libell\351", 1, 100 ); bvalid = false && checkLength(description, "Description", 1, 250); 

The moment it falls into false, it will stop executing this conditional.

0
source

All Articles