Monday, December 18

Boolean Logic For Javascript

Google+ Pinterest LinkedIn Tumblr +

JavaScript Basics – Part 5

Introduction
This is part 5 of my series, JavaScript Basics. In this part of the series I teach you a way of reasoning called Boolean Logic. This way of reasoning is used a lot in computing, programming, electronics and telecommunications, today.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Problem
Assuming that I am tall and you are tall. If someone ask you, if both of us are tall, you would say, Yes (true). If he asks if both of us are short, you would say, No (false). If you are short and I am tall, and he asks you if either you or me is tall, your answer would be Yes (true). If he asks if both you and me are tall, you would not have an answer. You might go on to say that the last question should not be asked or that the question does not have an answer. Well, I want you to know as from today that under certain circumstances the question should be asked.

In biology, a person is either tall or short. It is “environmental” conditions that make the person to have a medium height.  One scientist, George Boole defined a set of answers or rules for this kind of questions. We learn this rules in this part of the series. These rules are used in computing, programming, electronics and telecommunications, today. In fact, without these rules, you will not have a computer; you will not also have programming.

True or False
The simple human language statement is either true or false in itself. If I say, “I am tall”, that is either true or false. If I say, “you are tall”, that is either true or false. If I am tall and you are short, and the question is asked, if both you and me are tall, in Boolean Logic, an answer of true or false must be given. Which of these two should be given? Boole did not really answer this question. He simply came up with a set of rules for us to follow. The good news is that, when you follow these rules in their right context, you do not have any ambiguity. Thanks to these rules, we have computers and programming, today. I will give you the rules. The rules cannot really be explained; you just accept them. The rules are under three headings: AND, OR and NOT.

AND
The question can be asked, if both you AND me are tall. My height and your height are combined by AND. These are the rules to follow:

false AND false = false
false AND true = false
true AND false = false
true AND true = true

Now, let tall be true and short be false. This means that if I am short AND you are short, you and me are short. If I am short AND you are tall, you and me are short; that is Boolean answer, which you have to accept. If I am tall AND you are short, both you and me are short. If I am tall AND you are tall, you and me are tall. All these are Boolean rules that you have to accept.

OR
The question can be asked, if you OR me is tall. My height and your height are combined by OR. These are the rules to follow:

false OR false = false
false OR true = true
true OR false = true
true OR true = true

Again, let tall be true and short be false. This means that if I am short OR you are short, you or me is short. If I am short OR you are tall, you or me is tall. If I am tall OR you are short, you OR me is tall. If I am tall OR you are tall, you or me is tall. All these are Boolean rules that you have to accept.

NOT
Now, in Boolean logic, only two states (possible answers) exist. That is, if you are NOT tall, you are short; if you are NOT short you are tall; nothing else. These are the rules to follow:

NOT false = true
NOT true = false

Assume that you have a string (spring), you can extend (pull). While the string is in its natural state, if I say, “NOT short” you would extend it; that is the interpretation. While the string is extended, if I say, “NOT long” you would allow it to contract; that is the interpretation.

You have to memorize all the above rules under their different categories.

More than two Variables
In JavaScript AND, OR and NOT are each called an operator. For the NOT operator, you need only one variable to have an answer. For AND or OR, you can have more than two variables. The above cases show two variables. You can have three variables for AND; something like:

false AND false AND false = false
false AND false AND true = false

These are two lines. There are actually nine lines when the variables are three. When the variables are four, there are 16 lines.

You should consult some other text to know the pattern when the number of variables is higher than 2. It will not be difficult to identify the patterns. The pattern for AND and the pattern for OR are different.

True or false is actually a value and not a variable. I have referred to them as variables in this article, for simplicity.

I hope you now know what Boolean values (true or false) mean. We carry on in the next part of the series.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

Getting Started with JavaScript
JavaScript Outputs
Basics of JavaScript Variables
JavaScript Conditional Statements
Boolean Logic for JavaScript
Boolean Logic and JavaScript Conditions
Comparison and Arithmetic Operators
JavaScript Loop Statements
JavaScript Function Basics
JavaScript Object Basics
JavaScript Array
Some JavaScript Predefined Objects
Introduction to Form Field Validation with JavaScript
Some JavaScript Tips
 

Share.

About Author

Leave A Reply