Coding for Kids

Comparisons and Logical Operators

The computer needs to be able to make comparisons so it can then make decisions. There are many types of comparisons we can have, like if two things are the same, or if they're not the same, or if one value is bigger than another, and so on.

Every comparison returns a Boolean variable. That means it gets interpreted to be either true (if the comparison is correct) or false (if the comparison is not correct).

Equals Signs

This may sound silly, but we need to talk about the equals sign = and how it's used in JavaScript. There are three possibilities.

Assignment Operator =

This is a single equals sign. It is used to assign values to things. If you have a variable and you want it to hold a number, you assign it with a single equals sign. If you change a value, you also use this to change the value.

Anything to the RIGHT SIDE of the assignment operator (=) is evaluated FIRST. Once its value is known, then it is assigned to the variable on the LEFT SIDE. So, it's sort of working from right to left. It is important to understand this because this makes some expressions look like bad math.

In algebra class, this makes no sense (unless your age were Infinity). But this is perfectly valid in computing, and actually the way it must be done. Look at the RIGHT SIDE FIRST. It is saying to take the value of age (for Monty, it's 5) and then add 1 to it (so now it's 6). Now that we have the new value, set the variable age equal to the new value. It THEN becomes 6.

Equality Operator ==

Using this operator returns a Boolean value of true or false. It's made up of 2 equals signs (==) in a row with no space between them. Two of them, so I think of it as saying, "equal to."

But this comparison has some flaws in it. It checks for values only. Some coders use those flaws intentionally, but unless you understand type conversion (and even if you do), then this can lead to errors. For example, JavaScript will try to convert strings to numbers to make comparisons and it will consider the Boolean true as the number 1 and the Boolean false as the number 0. It makes other conversions too, but we're not going to get into them because we won't be using this for comparison at all.

Strict Equality Operator ===

I this of this as "really equal to" though its proper name is "strictly equal to". This operator prevents type conversion, so the number 0 and the Boolean false are not considered equal. For this comparison to test positive (true), the type of variable and the value must both match. It's better to always use this.

Logical Operators

Now that we know why we need to use === to see if two things are the same, let's talk about using these and other logical operators.

There are several logical operators that help the program do some interesting things. Some of these are mathematical comparisons. Others are purely for logical constructs to test multiple things.

Greater Than/Less Than (or Equal To) >, >=, <, <=

These work just like they do in your math class.
< means less than
<= means less than or equal to
> means greater than
>= means greater than or equal to

When you use these, you return a Boolean (true or false) based on how it evaluates.

NOT Operator !

This negates a true or false. In other words, it makes a true look like a false and it makes a false look like a true.

This does have many uses. Let's say you're testing the weather. If it is sunny out, then you're fine, but if it's raining, then you need a jacket. You could use something like this.

It's readable, isn't it? "If it is not sunny, then put on your jacket."

AND Operator &&

You need two ampersands && (shift+7) for this one. The AND operator allows you to test for two (or more) conditions at once. You put a comparison (or Boolean result) on each side of the && operator.

If BOTH sides are true, then this evaluates as true.

If even one side is false, then the whole thing is considered false. If the first term is false, it doesn't even look at the second term, which can be a helpful thing in some cases.

You can combine any number of these checks as you need.

OR Operator ||

You need two pipes || for this. Find the pipe above the enter key. It's on the same key as the backslash, but you need to use shift to get the pipe (shift+\).

The OR operator tests for two (or more) conditions.

If EITHER condition is true, then this evaluates as true.

If the first conditions is true, it doesn't look at any other conditions.

To get a false from this, all the comparisons must be false.

You can also group these together into larger and more complex comparisons using parentheses like in a complex math problem. But in most of those cases you probably want to break the code down into smaller chunks.

See if you can make sense of these.

Example 1:

If any ONE of those conditions is true, you should bundle up. If it's snowing or if it's windy or if it's hailing or if it's raining, bundle up!

Example 2:

You have to do ALL THREE of them to see the alert. You needed to study, you needed to try your best, and you needed a score of 65 or higher.

Example 3:

This one is a little more complex. Notice the extra parentheses around haveSnacksHere and fridgeIsFull? Only ONE of those needs to be true for that side to be true. On the other hand, you also need to be hungry otherwise you won't eat (you would get false). Here are all the possible outcomes in this truth table.

Coding for Kids
Sample truth table

It's a bit wacky and it can take some time to understand. Try to look through and make sense of it. You may not need it for most of your coding, but it could happen some day.

All things being equal, it seems logical that you keep learning!

—Dr. Wolf