Boolean Operators

Boolean operators are those that take boolean values as their inputs and return a boolean value as their result. There are three boolean operators in JavaScript - AND written as &&, OR written as || and NOT written as ! - with the first two of these taking two boolean values as inputs and the third taking just one.

The NOT operator is the simplest of these as it simply inverts the boolean passed to it returning true if it was passed false and false if it was passed true. AND only returns true if both values passed to it are true and returns false if either or both are false. OR returns true if either of both are true and only returns false if both are false.

You will notice that with both AND and OR conditions that the value to be returned is sometimes known just by looking at one of the two values. If one of the values is false then AND is false and if one of the values is true then OR is true. JavaScript takes advantage of this to reduce amount of code to be processed. JavaScript evaluates the left value first and then decides based on its value and the operator whether or not it needs to evaluate the right value at all. When AND is the operator and the left value evaluates as false JavaScript doesn't bother to evaluate the right operator at all. When OR is the operator and the left value is true then JavaScript doesn't bother to evaluate the right value at all.

Another thing regarding these two operators in JavaScript is that while the two values are converted to boolean for the purpose of evaluating the AND or OR operator, they retain their original values for other purposes. The NOT operator behaves differently in that the value returned will always be either true or false.

These aspects to the way that JavaScript processes these operators allows for some interesting uses beyond those of just comparing boolean values. Let's look at a few of these.


This applies the NOT operator to the value returned from the something() function twice. The effect is that regardless of what the function returns it will be converted to either true or false. Basically this is a shortcut for converting anything into a boolean. The first time the operator is applied it converts the result to a boolean but also reverses the value. The second NOT reverses the value again to give the boolean equivalent of the original value.

x = +x || 0

Using a unary + on a value converts it to a number or to NaN if it isn't a number. All of the values that this can produce except for 0 and NaN evaluate as true and so those values will be retained in x. When the +x evaluates as 0 or NaN then the right value is evaluated and while the operation still evaluates as false, the right value gets used in the assignment regardless as there are no more operators to be evaluated. This ensures that x is definitely a number as everything else gets converted to 0.

false && something() || somethingelse()

Because the left value of this AND operation is false the rest of the statement never gets run at all. The something() and somethingelse() functions are not even called.

true || something() && somethingelse()

Another example of where neither function will be called (even though && has higher precedence than || in JavaScript).

true && something() || somethingelse()

In this example the something() function will be run. If it returns true then the somethingelse() function will not run but if it returns false then the somethingelse() function needs to run in order to determine the final result.

Should you need to have the functions all run regardless of the results returned by the prior code then you would need to run all of the functions and assign the values returned to variables and then use those variables in place of the functions in the boolean comparison.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow