Sample Video Frame
14: True and False Tests
x is less than, equal to, or greater than another variable
y. There are a large number of comparison operations that all fall into the category of "boolean logic", but in programming we'll call this a test. The end result of performing a test is
x == y or
y != 1. Your computer's CPU even has a
TEST instruction that does this.
== (double equals) or
=== (triple equals). You can think of
== as "could be equal" and
=== as "definitely equal". You can play with
node and try these examples out:
> 1 == '1' true > 1 === '1' false > '' == 0 true > '' === 0 false
You can see that with
'' (an empty string). I explain this in the next part, but try to figure out why this happens in this example.
The inverse is to test whether two variables are not equal using
!== with the same rules.
!= can be used with more variable types than
!== more strict. This trend continues with all the equality operators: one = on the end means "could be equal" and two == means "definitely equal". You can also play with this operator too:
> 1 != '1' false > 1 !== '1' true > '' != 0 false > '' !== 0 true
Try different kinds of values on either side of the operators to see what you get. You notice how I'm showing you the comparison of
!== ends up being more what you'd expect from that kind of comparison.
Greater-Than and Less-Than
You can also test whether two values are greater or less than each other using
> (greater-than) and
< (less-than). Now, you might be asking, is there a strict version of these similar to
!==. No, there is not, so your comparisons will attempt to compare just about anything you give them. Here is me testing these two out in node:
> 1 < '2' true > 1 < 2 true > 1 > 3 false > 1 > '3' false > 1 > '' true > 0 < '' false > 0 > '' false >
You'll notice that
''. If you look at the end of this you'll see I compare
1 > '' which is
true, but then if 1 is greater than an empty string, why is 0 not considered less or greater than an empty string? We'll see in the next operator.
Greater, Less than, or Equal
The final comparison operators are
<= (less-than-or-equal) and
>= (greater-than-or-equal), and they do mostly what you think they do. If you remember from the demo above I had
> 0 <= '' true > 0 >= '' true > 1 <= '2' true > 1 <= '1' true > 1 <= 0 false >
You can see now that 0 is shown to be compared to '' correctly (as long as you overlook that an empty string probably shouldn't be compared to 0). I also show other comparisons using numbers and strings, but you should also play with this to understand it.
Logic operators take the result of two or more tests (as in
&& operator handles 'and' comparisons as in
x == 3 && j == 4 means "is x equal to 3 and j equal to 4?" The result of an and is true if both sides of the operator are also true. The
|| operator handles the 'or' comparisons as in
y == 4 or y == 10, which means "is y equal to 4 or is y equal to 10." Finally, the
! (not) operator inverts the truth of anything.
x != y && a < b.
You should have been trying out the tiny samples in the node shell and playing with logic and comparison operators as much as you can. Try to come up with your own complicated logic expressions and guess whether they will be true or false before you hit enter. If you watch the video for this exercise, you'll see me doing many of them using node as my tutor.
For this exercise, simply try these out in node:
5 < 10 && 7 > 9
3 > '5'
21 === '21'
78 <= 1023 && 45 > 23 || 34 < 23
7 >= 3 && 94 == 23 && 45 < 100 || 89 == 89
Also try adding in math to your logic practice:
6 + 34 < 2 * 20 && 78 - 23 > 100 - 30
2 + 3 * 67 <= 789
Also, try setting some of these to variables and see if you can work it out:
let x = 45; 5 + x < x && x * 20 > 34 * 2;
Do as much of this practice as you can as it will help you be more solid later.
Register today for the Early Access course and get the all currently available videos and lessons, plus all future modules for no extra charge.
Still Not Sure? Try the next FREE Lesson!