Learning to Code: Day 45 — Basic JavaScript Part 10

Hello everyone! Hope you are all keeping well. Today we’re going to be taking a focussed look at Comparisons in JavaScript, thanks as always to FreeCodeCamp for the lessons. Let’s get started.

The inequality operator ( written as !=) is the opposite to the equality operator and means to say something is “Not Equal”, returning as false, whereas the equality operator would state the opposite, and vice versa.

Like the equality operator, the inequality operator also carries out type conversion when making a comparison.

- FreeCodeCamp

Here we can see the inequality operator in action on line 2 for the if statement saying that if the condition val (tested as 10 on line 8) is not equal to 99, then the function testNotEqual will return “Not Equal”.

The strict inequality operator (!==) works in opposition to the strict equality operator (===), returning false where the strict equality operator would return true. It also does not carry out type conversion.

-FreeCodeCamp

Heads up, we’re about to get a little repetitive with the phrasing of these next chapters…

The greater than operator (>) compares the values of two numbers, returning true if the number on the left of the operator is greater than the number on the right, and false otherwise. Like the equality operator, it carries out type conversion.

The greater than or equal to operator (>=) compares the values of two numbers, returning true if the number on the left of the operator is greater than or equal to the number on the right, and false otherwise. Like the equality operator, it also carries out type conversion.

-FreeCodeCamp

The less than operator (<) compares the values of two numbers, returning true if the number on the left of the operator is less than the number on the right, and false otherwise. Like the equality operator, it carries out type conversion.

The less than or equal to operator (<=) compares the values of two numbers, returning true if the number on the left of the operator is less than or equal to the number on the right, and false if the number on the left is greater than the number on the right. Like the equality operator, it carries out type conversion.

- FreeCodeCamp

The logical and operator (&&) allows you to test more than one thing at a time. If you want to parse faster through data as you code, the logical and operator can come in handy here.

Take this example from FreeCodeCamp where an if statement was nested inside another if statement:

-FreeCodeCamp

The logical and operator can be used here to return true if and only if the operands (the conditions in this case) on either side of it return true. Let’s take a look:

-FreeCodeCamp

So, any number outside of 5–10 (excluding 5 and 10) will return “Yes”.

Lastly, the logical or operator (||) returns true when the operands on both side of it are also true, like the logical and operator.

-FreeCodeCamp

Here, the code returns “No” if the num we test later is greater than 10 or less than 5. So we are thinking about a value from 5–10 (5 and 10 included) for “Yes” to be returned. We can summerise this code shorter where or is concerned:

-FreeCodeCamp

Aaaaand let’s call it a day there. Phew! That was a lot of comparisons for one day. See you guys next time!

Step by step, learning to code with FreeCodeCamp