Javascript Jems - Active Logic, Truthy and Falsey
Written by Ian Elliot
Thursday, 29 November 2018
Article Index
Javascript Jems - Active Logic, Truthy and Falsey
Lazy Evaluation & And
The Ternary Operator

## Lazy evaluation

There is yet another small and subtle point.

Consider:

`A || B`

Notice that if the first value is truthy then it's value is returned and the value of the second term is irrelevant. JavaScript takes notice of this fact and saves time by not even bothering to evaluate the second term if it isn't needed.

This is more generally called lazy evaluation.

For example:

` A=1; A=A||test("function called"); function test(a){ alert(a); }`

As A is truthy there is no need to evaluate the second term and hence the function test is never called. On the other hand if A=0 or any other falsey value the function is called.

This sort of lazy evaluation can help you avoid runtime errors.

The basic idea is that you write something as the first expression that evaluates to truthy when the second expression should not be evaluated.

For example, JavaScript throws a runtime error if you try to access a method on a null object:

` A=null; A.method();`

but we can easily check to see if a variable is null using:

`!A||A.method();`

in this expression !A. i.e. not A, is truthy if A is null and hence we never evaluate A.method if A is indeed null.

You can avoid using the ! (NOT) operator (see later) by expressing the same idea using the && operator as will now be explained.

## AND &&

All of the ideas that we have just encountered with respect to the logical Or operator || apply to the And && operator.

The usual truth table for AND i.e. && in JavaScript is:

 A B A&&B F F F F T F T F F T T T

which we now have to interpret as truthy and falsey values as for the Or operator.

You can also see that you can reinterpret the table in an "active" way and formulate the AND operation as -

if the first expression is falsey then the result is the first expression.

If the first expression is truthy then the result is the second expression.

You could interpret it as saying:

"return the first value if it is false and return the second value otherwise".

Notice that is just the way we work out the logical OR

"return the first value if it is true and return the second value otherwise".

but with the first expression false rather than true.

You should also be able to see the the AND operation

`result = A&&B;`

is equivalent to the if statement:

`if(A){ result=B}else{ result=A};`

If you think about this for a moment you should be able to see that (!A)&&B is "almost"  the same as A||B in that both give the value B when A is false.

However it isn't identical because when A is true (!A)&&B gives the logical opposite of A||B that is (!A)&&B is false but A||B is true if A is true.

The exact equivalence is given by one of De Morgan's laws of logic and it reveals that

`(!A)&&B  = !(A||!B)`

You can use the AND operator in much the same way as the OR operator and it too uses lazy evaluation - that is if A is false then B in A&&B is not evaluated.

This enables us to write the protection against a runtime error given earlier as:

` A=null; A&&A.method();`

and avoid the use of a NOT !.

<ASIN:1871962579>

<ASIN:0596517742>

<ASIN:1590597273>

<ASIN:059680279X>

Last Updated ( Thursday, 13 June 2019 )