How to Compare With Greater Than, Less Than, Equal To, and Not Equal To Operators in JavaScript (Easy!)

I’ve stated this before, and I’ll say it again.

  • Uppercase letters are less than lowercase letters.
  • JavaScript reads from left to right.
  • The size of the letters gets bigger from “a” to “z.”

But here’s something interesting.

It turns out that capitalization takes precedence over the actual order of the alphabet!

So if you’re comparing letters of the alphabet, you need to first consider whether or not it’s capitalized, and then consider the order where that letter is in the alphabet.

How to use the “greater than” operator

The greater than operator is symbolized as >.

Here’s an example of a true expression using >.

Example 1: How to use the “greater than” operator in a JavaScript expression

console.log("z" > "a");

And the output is:

true

How to use the “less than” operator

The less than operator is symbolized as <.

Check out the example below.

Example 1: How to use the “less than” operator in a JavaScript expression

console.log("Z" < "a");

And the answer is:

true

Surprise! Didn’t I tell you above?

First, consider the capitalization, then the position of the letter in the alphabet.

In that case, a capital Z is smaller than the lowercase a.

There’s nothing more to it.

And that’s what < means.

“Z is less than a.”

True.

How to use the “equal to” operator

The equal to operator is symbolized as ==.

I think this one is as clear as day.

Being equal to is exactly as it sounds. If it’s not exactly the same, JavaScript won’t recognize it as the same.

The two things have to be exactly the same, and that also means you have to take into consideration the capitalization.

Let’s show you an example:

Example 1: How to use the “equal to” operator in a JavaScript expression

console.log("a" == "a");

The output is:

true

Easy!

If it were anything else other than “a” compared to “a,” it’d be completely false!

Example 2: The exception is that NaN is not equal to NaN!

console.log(NaN == NaN);

You probably already guessed what the answer to this is.

false

But why is it false? Well, apparently that’s just how JavaScript is.

A NaN (Not a Number) is the only value in JavaScript that isn’t equal to itself.

NaN is suppose to be a number but nonsensical. In essence, it’s one NaN isn’t like any other number or NaN either.

Just remember that.

How to use the “not equal to” operator

The not equal to operator looks like this !=.

I think most of us get the point.

console.log("a" != "A");

And the output is:

true

Yep! That’s exactly what we’re expecting.

Leave a Comment