Tuesday, July 27, 2010

Javascript - boolean logic

In javascript, to find out if certain number of conditions of a set of conditions are true. Say, there are four conditions, c1, c2, c3 and c4, and we need to carry out some logic if, at least "two" of these conditions are true. We can do this in the following way:

eg:

<script>
function isConditionTrue(c1,c2,c3,c4) {
if ((c1 + c2 + c3 + c4) > 1) {
return true;
}
return false;
}

alert(isConditionTrue(true,false,false,true));
</script>


This method will return true as two of the conditions are true.
Note: the boolean values are treated as numbers(false - 0, true - 1) when used inside algebraic expression.