|Written by Ian Elliot|
|Wednesday, 04 September 2013|
Page 1 of 3
The traditional way of thinking about Boolean or logical operators is in terms of a truth table.
with T=true and F=false.
If you try this out using:
you will see that the result is indeed true as the line starting T,F suggests. Notice that thinking about logical operations as truth tables makes them seem somehow static - and certainly not part of the flow of control of a program. However this is just an illusion. Logic functions and branching of the flow of control are deeply related - so much so that they are the same thing.
The values zero, null, undefined and the null string are falsey and they behave in logical expressions as if they were false. Everything else is truthy and they behave in logical expressions as if they were true.
So for example you can write
and still see the TRUE message displayed because A is set to a truthy value and B is set to a falsey value which results in a truthy value.
However things are even stranger than just truthy and falsey values. There is another way to interpret Boolean logic that gets closer to the active or procedural notion of programming.
Take a look at the logical table for OR again:
You could interpret it as saying
"return the first value if it is true and return the second value otherwise".
In more usual programming terms:
When you first see this form of the OR truth table it comes as something of a shock. We are so familiar with thinking of logical operators as just that - operators - that to see OR expressed in "if then else" form is almost disturbing. However you can't object as it really does work - try it out and see.
This rule gives you exactly the same truth table when the values are Boolean but consider what you get if the first value is numeric with 0 i.e. falsey and 20 i.e. truthy and the second is a string with "" and "Hello" being falsey and truthy respectively:
This looks very strange but you can see that A||B returns B if A is falsey and A if A is truthy.
Using the if statement equivalent of A||B makes what is happening clear:
is the same as
displays 21 because the first variable is truthy but:
displays "hello" because A is falsey.
This last example also gives us some idea of what this could be use for - default values. For example, consider the following function:
If you don't remember to, or don't want to, set the a parameter then it is undefined when the function starts to run and hence it is falsey and a is set to "default value". That is:
results in "default value" being displayed. If you set the parameter then it is a truthy value and a remains unchanged. Notice however that if you pass in a falsey value such as the null string "" or 0 then these will also be replaced by the default value which might not be not what you intended.
|Last Updated ( Thursday, 05 September 2013 )|