Skip to main content

JavaScript Equality : Double Equals v/s Triple Equals

Published 4 min readMarkdown
JavaScript Equality : Double Equals v/s Triple Equals

One thing we often do as a programmer in any programming language is Equality Checking. Whether this value is equals to that value or not.

Whether you know or not in JavaScript there are two equality checking operators: double equal(==) and triple equal(===). And they often leads to programmer to be confused while using them.

We'll they are not too complicated to understand.

In this article we'll go over some differences and use cases like where and how to use them efficiently and guess what they are also funnier to learn.

So let's go πŸš€.

But hold on before deep dive in this, there is one thing I want you to take below points in your consideration.

  • All the comparison operator returns Boolean Values after execution. Either true or false.

  • And as we all know in programming there is only two values 1 and 0. So if we go even further, true become 1 and false become 0.

Okay then, with that keep in mind let's get into this.

In JavaScript the comparison happen in two ways.

  • Comparison with type
  • Comparison with values

So what's the difference between == and === πŸ€” ?

One simple answer πŸ‘‡

The Double Equals first convert the type of the operand and then compare them with values. While the Triple Equals compare with the value without changing the type of the operand.

So that's it ? πŸ˜’

Hell not. There is more yet to come.

πŸ”” But wait, if you don't know about type conversion in JavaScript. Do check the article below πŸ‘‡. It would make much more sense then:

Now, Let's look upon some scenarios :

To check whether a value is true or false, we can use Boolean object constructor. Here's how πŸ‘‡

console.log(Boolean('hey'))  //true
//-- any valid string is true
console.log(Boolean(0))       //false
//-- as I said earlier 1 is true and 0 is false
console.log(Boolean('0'))     //true
//-- why 0 is true here ? Thanks to quotation, which makes it a String
console.log(Boolean(' '))      //false
//-- the quotation has no character: not a valid string; false
console.log(Boolean([ ]))     //true
//-- empty array

More examples:

console.log(false == 0)  //true
console.log(0 == '')  //true
console.log('' == false)  //true

Double Equal converts false and ' ' into 0, that's why they are equal to 0.

But !, this not gonna happen in case of Triple Equal. Why ? Cause === not converts the type of operand.

console.log(false === 0)  //false
//-- false is Boolean while 0 is Number, so they not equal for ===
console.log(0 === '')  //false
//-- 0 is Number while '' is string
console.log('' === false)  //false
//-- '' is String while false is Boolean

✨ Data type is one of the crucial topic in any programming language; and so as in the JavaScript. I have also written an article on Data Type in JavaScript. You can check it here πŸ‘‡

With that said, let's move further. In JavaScript we have: null, undefined and NaN

  • null is a type of object, which mean noting; blank
  • undefined is a data type itself
  • NaN is type of Number, which mean not a number
console.log(typeof null) // object
console.log(typeof undefined ) // undefined 
console.log(typeof NaN) // Number

So first, let's compare null and undefined with == v/s ===;

 console.log(null == undefined) // true
//-- double equal convert null into 0 and undefined as well

 console.log(null === undefined) // false
//-- for triple equal null is an object while undefined is undefined

null and undefined are only equal to each other and themselves while comparing with Double Equal( == ). If you compare them with any other value you'll get nothing but a false.

console.log(null == null) //true
console.log(null == ' ') //false
console.log(null == false) //false
console.log(null == 000) //false
console.log(null == 123) //false
console.log(null == []) //false

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

Now is the time for NaN

NaN is a insane player in the JavaScript world. Why? Cause it never equal to any value and guess what ! It not even equal to itself.

Are you kidding me ? 😡 No buddy, have a look πŸ‘‡

console.log(NaN == null) //false
console.log(NaN == 0) //false
console.log(NaN == 135) //false
console.log(NaN == 'NaN') //false
console.log(NaN == 'hellow') //false
console.log(NaN == 0) //false
console.log(NaN == undefined) //false
console.log(NaN == NaN) //false

Wrapping Up:

So as you saw, one can easily get confused while choosing whether he/she use == or ===.

Let me clarify this. Whenever you need to compare two values always go with ===, cause it gives expected results. But do play with both == and ===, cause programming is fun right ?

So that's it for now.

Checkout my other article here πŸ‘‡

Connect with me on twitter :

I'll see you is next one. 😎

About the author

No comments yet. Be the first to comment.