Back to blog

Equality Comparisons in Programming

Hello HaWkers! Jeff Bruchado here.

This is another article in our series that will cover our journey to Learn Programming from Scratch.

Today will be an article about another extremely important content to proceed with your learning.

So please! If you don't have mastery over what I'll talk about today, read to the end!

Well, without further ado, let's get started!

Today, we're going to talk about a super used topic in Programming: Equality Comparisons.

Equality Comparisons are used to check if two values are equal or different.

There are two ways to make equality comparisons:

The loose equality operator (==) and the strict equality operator (===).

Let's understand each of them in detail.

Loose Equality Operator (==)

The loose equality operator (==) is used to check if two values are equal.

Before making the comparison, the loose operator tries to convert the values to the same type before making the comparison.

For example:

console.log(1 == 1); // true
console.log('1' == 1); // true
console.log(true == 1); // true
console.log(null == undefined); // true
console.log([] == false); // true

As we can see in the example above, the loose equality operator (==) tries to convert the values to the same type before making the comparison.

In the first example, 1 is compared with 1 and both are of the same type (number), so the comparison is true.

In the second example, '1' (string) is compared with 1 (number) and they are converted to the same type (number) before comparison, so the comparison is true.

In the third example, true (boolean) is compared with 1 (number) and they are converted to the same type (number) before comparison, so the comparison is true.

In the fourth example, null (a null reference value) is compared with undefined (an undefined reference value) and they are considered equal, so the comparison is true.

In the fifth example, [] (an empty array) is compared with false (a boolean value) and they are converted to the same type (number) before comparison, so the comparison is true.

Well HaWker, as you can see, this content has a relatively high density of information.

And it's common to feel confused with lots of information.

So remember, if you have questions, always send me your question as a reply to these articles.

I'll always try to help you in the best way possible.

I'm here to help you evolve and progress in your career.

I'm working hard to ensure I can pass on all the necessary information concentrated all within this article.

But if you want access to something more, and have access to the complete content of the entire sequence you'll receive over time.

You can click on this link, no commitment, I emphasize that this is only **if you really want to accelerate your development through practical exercises and all content organized and separated in one place, divided by priority levels, between what you need to learn and what's not so necessary at the beginning of your journey.

I guarantee this will greatly accelerate your studies.

Anyway, having given the message for those who want to accelerate their development in the field, let's continue with the content and understand the Strict Equality Operator (===).

Strict Equality Operator (===)

The strict equality operator (===) is used to check if two values are equal and of the same type.

It doesn't try to convert the values to the same type before making the comparison.

For example:

console.log(1 === 1); // true
console.log('1' === 1); // false
console.log(true === 1); // false
console.log(null === undefined); // false
console.log([] === false); // false

As we can see in the example above, the strict equality operator (===) doesn't try to convert the values to the same type before making the comparison.

In the first example, 1 is compared with 1 and both are of the same type (number) and value, so the comparison is true.

In the second example, '1' (a string) is compared with 1 (number) and, since they are of different types, the comparison is false.

In the third example, true (a boolean value) is compared with 1 (a number) and, since they are of different types, the comparison is false.

In the fourth example, null (a null reference value) is compared with undefined (an undefined reference value) and, although they are considered equal by the loose equality operator, they are of different types, so the comparison is false.

PS: If you don't understand the different Data Types, access one of the previous articles that talks about Data Types and reread it.

In the fifth example, [] (an empty array) is compared with false (a boolean value) and, since they are of different types, the comparison is false.

Conclusion

In summary, the strict equality operator (===) is generally considered safer than the loose equality operator (==), because it doesn't try to convert values to the same type before comparison.

It's recommended to always use the strict equality operator, unless you have a good reason to use the loose equality operator.

Well HaWker, we've reached the end of another article.

I hope this article has added to your repertoire of programming knowledge.

As always, if you have any questions or want to explore any point further, just reply with your question to this email.

And remember, I'm here to help you improve 1% with each new article!

Awesome HaWker! 👏🏻

If you made it this far, as usual, please reply to this email with "I managed to understand Equality Comparisons in Programming".

This way I'll know that this article series is being useful for you, and that you're following along and taking advantage.

As you already know! Only a true HaWker makes it to the end.

Congratulations!

I'm happy to have you here.

See you soon.

Let's go! 🦅

- Jeff Bruchado

Congratulations on making it this far, HaWker! 🎉

Now that you've learned about equality comparisons in programming, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.

It's a great way to reinforce learning and prepare for the next steps in your programming journey.

Click to access the quiz.

Good luck and have fun!

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments