Javascript Jems - Active Logic, Truthy and Falsey
Javascript Jems - Active Logic, Truthy and Falsey
Written by Ian Elliot   
Wednesday, 04 September 2013
Article Index
Javascript Jems - Active Logic, Truthy and Falsey
Lazy Evaluation & And
The Ternary Operator

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 

 

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

 

 
 



Comments




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

 

Banner


Date Hacks - Doing JavaScript Date Calculations

OK, you have mastered the way your particular language represents dates and times but.. this is just the start. Doing arithmetic with dates can go well beyond just working out the interval between two [ ... ]



Just JavaScript - How Functions Become Methods

In this chapter extract the aim is to show how Function objects are used as methods by other JavaScript objects. Methods aren't just functions, they are functions that work with the object they are bo [ ... ]


Other Articles

 

<ASIN:1430230541>

<ASIN:0321683919>

<ASIN:0596517742>

<ASIN:0321572602>

<ASIN:0596806752>

 



Last Updated ( Thursday, 05 September 2013 )
 
 

   
Banner
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Banner