Apples to Apples, or How to Compare Stuff ("Lucky Seven" Project #3)

Programming almost always involves comparing some value to another. It’s a fact of life, er, coding (Is there a difference?), and it’s not difficult to do. You compare things in your head all the time, and I’m sure you’ve compared numbers in math class before. Now let me show you how to compare things in Javascript.
(I’m also going to include some logic stuffs, which also isn’t difficult. I’ll give examples for everything, so don’t worry.)
By the way, the == operator tends to confuse beginning programmers until they’re ripping their hair out, but there’s no reason to fear it. Just make sure you understand the difference between == and =. Ask me questions about it if you don’t 🙂

>
I know you took algebra in middle school. This means the same thing, “greater than,” and is used to compare two values.
So…
9>7 is obviously true.
(Yes, this stuff sounds ridiculous right now.)

<
Same thing. This one means "less than."
7=
“Greater than or equal to.”
This is the same thing as the > with a line under it, which you’ve probably seen before.
9>=5, and 9>=9

<=
"Less than or equal to."
And this is the same thing as the < with a line under it, as you probably guessed.
5<=9, and 5<=5

!=
This is how you say "not equal" in Javascript.
This is probably the first one that you don't recognize. The exclamation point, sometimes called "bang" in computer science jargon, is the symbol for "not."
5!=2

==
This is how you say "equals" in Javascript. BEWARE! This is not how you make a variable (more on those later) equal to something else, for example x=5. If you say x==5, x won't equal 5. x==5 is checking to see whether or not x already equals 5.
5==5
if 5==5
then say that they are equal
otherwise (else)
say that they are not equal

NOT

x+2=6
therefore,
x==4
DON'T DO THAT. IT'LL MAKE ME CRY. And you 😀

&&
This is how you say "and" in Javascript.
if (you don't ever show up for class)&&(you don't ever study)
you're probably going to fail

if (you like what you're reading)&&(you like to have fun)
major in computer science

Note that both of these conditions MUST be true in order for the next line to happen. If you like to have fun but you hate what I talk about in my tutorials, don't bother majoring in computer science. If you never show up for class but you study lots, you might just pass. Yes, I know these examples aren't that great, but if you'd like then I can pull out my old discrete math textbook and find you some better ones that will give you a good idea of how these logic things work.

||
This is how you say "or" in Javascript.
if (you are reading this)||(you want to make me feel good about myself)
comment on some of my stuff /*Just kidding… Maybe.*/

if (you have red paint)||(you have yellow paint)||(you have blue paint)
you have a paint that is a primary color
//Note that you can compare more than two things, like in this example.

Unlike the &&, only ONE of these conditions has to be true. Both and/or all of them can be true, but it's not necessary in order for the second line's conditions to be true or carried out.

I know that this post doesn't have much in it words-wise, but comparisons are actually pretty important for what you're going to be doing. Make sure you're familiar with them, and post comments if you want better examples or if anything is unclear.

Comments are closed.