Comparison of values in JavaScript

In JavaScript, there are multiple ways to write conditionals. The first way most new developers get introduced to and get comfortable with is the “if statement”, “else statement” and the “else if” statement.

Hi 👋 and welcome to the second part of the mini-series of conditionals in JavaScript. In the last post in the series, I showed you how the If, Else and Else-If statement works. This short post will give you some more ways to use them more effectively, let's get started!

If statements always get executed if the value in between the parentheses is true, the same goes with else-if statements. So here is where comparison operators come into play. They are awesome and as a developer, you will use them all the time when you want to compare two strings or numbers.

Larger than: if (income > 100000)

Smaller than: if (income < 100000)

Larger than or equal to: if (income >= 100000)

Smaller than or equal to: if (income <= 100000)

Loosely Equal to: if (income == 100000) (note the double equation mark: ==)

Strict Equal to: if (income ===100000) (note the tipple equation mark: ===)

Not equal to: if (income != 100000)

Not very difficult to understand, right? Well, the == and === might be something that seems a bit wired at first glance, so let us dig a bit deeper.

What are the differences? The == JavaScript convert the type data type to match the comparing values. See the following example below:

console.log("10" == 10); *// true*

console.log("10" === 10); *// false*

The === checks if the values are the same and of the same type, that is why the console.log will return false in the example above.

When should I use Strict Equal To? For most use cases, the === is the correct comparison operator to choose, for all values that are not numbers since numbers are only equal to themselves. The only time the === operator can be wrong when dealing with numbers are with Signed Zeros, negative zero (-0) and positive zero (+0).

Read more: