operators Archives - Michiel Arkema's Blog https://blog.michielarkema.com/tag/operators/ The Official blog of Michiel Arkema Tue, 11 Apr 2023 12:40:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 214496708 What’s The Difference Between The equal-to & The strict-equality Operator In JavaScript? https://blog.michielarkema.com/javascript-secrets/whats-the-difference-between-the-equal-to-the-strict-equality-operator-in-javascript/ https://blog.michielarkema.com/javascript-secrets/whats-the-difference-between-the-equal-to-the-strict-equality-operator-in-javascript/#comments Tue, 11 Apr 2023 12:40:20 +0000 https://blog.michielarkema.com/?p=548 In JavaScript, we have two main operators to directly check if two values are equal to each other. (Excluding the >=, and <= operators). One is the == operator, and…

The post What’s The Difference Between The equal-to & The strict-equality Operator In JavaScript? appeared first on Michiel Arkema's Blog.

]]>
In JavaScript, we have two main operators to directly check if two values are equal to each other. (Excluding the >=, and <= operators).

One is the == operator, and the other is the === operator.

But what are the differences between the two and when you should choose which?

Well, if you read this blog post till the end, you’re going to discover exactly that.


The Equal-To Operator

The first and most common operator is the equal-to aka the == operator.
It allows you to compare two values with each other and receive a Boolean as a result.

Example:

const name = "Jack";
console.log(name == "Jack"); // This should print out 'true'.

The operator takes two values, one on the left and one on the right.

But there’s a caveat with this operator. And that it doesn’t matter if the values are not of the same Datatype.

Example:

const num = 1;
console.log(num == "1"); // It would still print out 'true'.

But why is that?

Well, the equals-to-operator checks both values loosely. Meaning that it won’t care if both data types are not the same. All that matters is that the value provided has the same character.

This is not the case with the next operator.

The strict-equality operator

Just like the equal-to operator, it checks if both values are the same and if that’s the case. It will either return true or false.

But here’s where this operator is different.

For starters, the operator uses three = characters. ===

Instead of ‘loosely’ comparing the two values, it will strictly compare them.

This means that the datatypes of both values must be the same as well as the values.

Example:

const num = 1;
console.log(num === "1"); // Now, it would print out 'false'

This feature allows you to write safer code and more accurate code that’s also easier to read.

But how can I choose which?

Personally, it depends on your preference as a developer. I’ve seen many people use the == operator and others use the === operator.

It also depends on how strict you want your code to be.

Although I do have to mention that the === operator allows for more concise and safer code because it will prevent errors like trying to manipulate a number value while it’s a string. (Like in the example shown above.)


So that was the post. You should now have a clear understanding of what the differences are and when to choose which.

Let me know in the comments down below if you found this post helpful.

Oh and remember, your dream career is just one step away!

Michiel Out!

Btw, I recently opened a FREE giveaway where 5 lucky winners will receive two weeks of video materials for FREE, from my JavaScript Mastery Blueprint.

It’s a must-have for anyone who wants to become a professional JavaScript developer.

You can join here: https://michielarkema.com/jsmb-giveaway

The post What’s The Difference Between The equal-to & The strict-equality Operator In JavaScript? appeared first on Michiel Arkema's Blog.

]]>
https://blog.michielarkema.com/javascript-secrets/whats-the-difference-between-the-equal-to-the-strict-equality-operator-in-javascript/feed/ 1 548