Javascript Basics: Accessing logic

April 26 2009, 9:14pm

Doing Arithmetic:

Operator What
&& Logical AND
|| Logical OR
! Logical NOT

&& will evaluate two things and return only if both are true, otherwise the && will return false.

The || will evaluate two conditions and return true if either one is true

The ! returns the inverse of a value.

    function init()
    {
     var yes = true, no = false;

 var blnA = yes && yes ;

 var blnB = yes && no ;

 var blnC = no || yes ;

 var blnD = no || no ;

 var tog = !yes ;


 var str = "Are both values true?: " + blnA ;
 str += "<br>Are both values true now?: " + blnB;

 str += "<br>Is either value true?: " + blnC;
 str += "<br>Is either value true now?: " + blnD ;

 str += "<br>Initial value: " + yes ;
 str += "<br>Toggled value: " + tog ;

 document.getElementById("panel").innerHTML = str;

}
window.onload=init;

This will create the following: