JavaScript Comparison Operators == and ===

JavaScript Comparison Operators == and ===

·

3 min read

You are here then either you are curious or you are confused. You have been using it for a while but you don't know, what's the actual difference between them. The same was the case for me. I used any one of them while creating programs. It didn't matter to me.

Later I started using triple equals === because it looked cooler than double equals ==. Once, while using react I stumbled upon a warning message when I used double equals and the warning message was gone as soon as I used triple equals. That's when I started thinking about the why behind this.

When asked that what's the difference between double equals and triple equals our cohort teacher told us that it can be a good topic for your blog and then again I had an internship interview, where I was asked about the difference between these. So, here I'm writing about it.

I have already written a tweet thread on this topic, which you can check here. But if you want a more elaborate explanation. You can continue reading.

Let's forget all that and get started with both of them.

Double equal comparison operator

How to use double equal operator

function myFunc(a, b) {
  if (a == b) {
    return console.log('A is equal to B.')
  }
}
// This function will log 'A is equal to B' in the following callbacks
myFunc(1, 1)
myFunc(2, '2')
myFunc('3', 3)
myFunc('4', '4')

All the callbacks you see above will log 'A is equal to B' because the double equals only care about the value and not about the data type. It can be a Number-Number, String-String, or Number-String. It doesn't matter to the double equals operator. The condition will return true and execute the set of code that is written in that if scope.

Triple equal comparison operator

By double equals operator, you must have got what is this triple equals operator. If not I'll tell you.

Let's see a function with triple equals condition

function myFunc(a, b) {
  if (a === b) {
    return console.log('A is equal to B.')
  }
}
// This function will log 'A is equal to B' in only in these callbacks
myFunc(1, 1)
myFunc('4', '4')
// All other than that will result in false
myFunc(2, '2')
myFunc('3', 3)

The other callbacks return false because the triple equals operator checks for both, the value itself and the data type. It will never output true if the value is equal and the data type isn't. It must be a Number-Number or String-String. No Number-String allowed.

So to execute your set of code in the if scope, you need to provide proper value and data type.

Conclusion

This was all about double and triple equals operators. Go ahead and experiment with it in your programs.

I would like you to tell that I regularly tweet out stuff related to development. You can check it on Twitter.

Now, if you enjoyed learning about this then do share it with everyone who might find this helpful.

Chao till the next one.

Priyanshu

Did you find this article valuable?

Support Priyanshu by becoming a sponsor. Any amount is appreciated!