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

The NOT !

Finally we come to the NOT ! operator.

This works in the same way as the other logical operators but it only has a single operand and also works with truthy and falsey values:

A !A
T F
F T

 

It is also worth knowing that it converts all truthy and falsey values into false and true i.e. proper Boolean values. So !null is true, !0 is true and so on. So while:

"Hello" || 0

is "Hello" 

!"Hello" && 0

is false. If you think about it what other interpretation of !"Hello" could you use in this case. 

So Not converts all truthy and falsey values into Boolean values. 

For example:

A=null;
if(!A) alert("A is null");

will flag the fact that A doesn't exist.

Of course there is no lazy evaluation with a NOT as it has only one operand and that has to be evaluated to work out its truth value. 

 

Banner

 

Ternary expression

You may have encountered the JavaScript ternary expression before but always regarded it as something special and not part of the whole logic of the language but seen in the light of the way AND and OR are implemented it makes a lot more sense. The ternary expression is just another example of active logic. That is:

  • AIIB     returns the second expression if A is false
  • A&&B  returns the second expression if A is true
  • A?B:C returns B if A is true and C if A is false

In terms of a truth table this too is just a logical function but one with three inputs:

 

A B C A?B:C
F F F F
F F T T
F T F F
F T T T
T F F F
T F T F
T T F T
T T T T

 

In this case it is the "active" interpretation of the logic that makes more sense than the "passive" truth table.

We have already noted that the AND && and OR || operators are the equivalent of if statements. That is:

result=A&&B;

and

result=A||B;

are the same as

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

and

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

It is interesting to notice that the ternary expression is also the equivalent of an if..else statement. That is:

result=A?B:C

is the same as

if(A){
 result=B

}else{
 result=C
};

This also means that the if and the if..else statements are just  logical functions with the truth table given earlier.

This is not the way we usually think about them.

Active logic and lazy evaluation make a great deal of sense out of some aspects of JavaScript that might otherwise look strange and arbitrary. JavaScript isn't the only language that has a ternary operator and understanding it in terms of active logic makes it seem much more natural. 

 

 

 

Related Articles

True but not true       

Dangerous Logic - De Morgan & Programming       

JavaScript Bit Manipulation 

Now available as a book from your local Amazon.

JavaScript Jems:
The Amazing Parts

kindlecover

JavaScript Jems - Functional And Not Quite Functional

Original drafts of chapters.

Contents

  1. JavaScript Patterns 
    Why JavaScript is a Jem
  2. Objects with Values in JavaScript*
  3. Private Functions In JavaScript
  4. Chaining - Fluent Interfaces In JavaScript*
  5. Active Logic, Truthy and Falsey*
  6. The Confusing Comma In JavaScript*
  7. Self Modifying Code*
  8. Lambda expressions
  9. Meta Programming Using Proxy
  10. Master JavaScript Regular Expressions
  11. The Function Approach To Programming

<ASIN:1871962579>

<ASIN:1871962560>

<ASIN:1871962501>

<ASIN:1871962528>

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 



Last Updated ( Thursday, 13 June 2019 )