Lesson 4.2: Arithmetic and Operators – Boolean and String Arithmetic
In our previous lesson, we went over some of the basic operators of Javascript and discussed numeric arithmetic. In this lesson we are going to talk about boolean and string “arithmetic”.
What are Booleans?
Booleans are variable types that only have two possible values: true and false. This are most commonly used in relationship to conditional control statements, which are used to control the flow of the code.
Boolean-Related Operators
There are several operators related to booleans that we are going to discuss: == (Boolean EQUALITY), || (Boolean OR), && (Boolean AND), < (Boolean LESSER THAN, > (Boolean GREATER THAN), <= (Boolean LESSER THAN OR EQUAL), >= (Boolean GREATER THAN OR EQUAL), ! (Boolean NOT).
Equality Test
The equality sign is a double-equal sign so it can be distinguished from the assignment operator (=) which we use to assign values to variables. This operator is used to compare any two values and determine if they are equal to one another. If they are, the result is true; if they are not, the result is false.
For example, say we have a numeric value and we want to see if it equals 5, we would do:
var result = (myNum == 5);
This would compare myNum to 5. If they are equal, it would assign result the value of “true”, otherwise it would assign the value of false. We will also use these type of equations in control structures such as if statements, which will look something like this:
if(myNum == 5)
alert("It is 5!");
else
alert("It isn't 5.");
We’ll discuss if statements, as well as the other control structures in our next lessons.
Keep in mind that the equality operator can compare anything, not just numbers. It can compare strings, booleans, or any other type of variable or object.
AND/OR
The Boolean AND and OR operators are similar in that they compare two boolean values together and give another boolean value. The difference is in what they return. The Boolean AND operator returns true if both the boolean values on the left and right side are true. The Boolean OR operator will return true if the either the value on the left or right are true.
These can be used to test multiple conditions together. You can link multiple AND/ORs together into one long statement. Each has the same level of precedence in the equation, so they are simply worked left-to-right.
For more on Boolean operators, take a look at the Programming lesson on Boolean Arithmetic. The only difference is that Javascript uses && instead of AND and || instead of OR. Also, there is an XOR mentioned. Javascript doesn’t have an explicit XOR operator, however this is equivalent:
var result = (a || b && !(a && b)); // Same as (a XOR b)
It isn’t a common occurrence to need an XOR, but it is good to know how to for that one time that you do.
String “Arithmetic”
Okay, you got me, technically you can’t do arithmetic with strings. But, you can still add them together (called “concatenation”). This will simply append the first string to the end of the second string, like so:
var string1 = "I am "; var string2 = "Bob"; var string3 = string1 + string2; // string3 now equals "I am Bob";
We’ll discuss strings more in-depth in a future lesson all about strings.
In this lesson we discussed the various types of arithmetic that you can perform in Javascript. In our next lesson we’ll take a look at control statements, which will let us control the flow of our code based on the values of variables.




