Difference between == and === operator in javascript

difference between == and === operator in JavaScript
Share this:

Every js developer knows that == compares value while === checks for value and type. This article is also going to say that but in a little comprehensive way.

Moreover, In many interviews, interviewers repetitively ask at least one question related to this in an interview.

Trite questions look like:

While some interviewers may want to ask the questions that most of the developers fail to answer. One of that kind is : 

What is the output of both console.log ?

Search for the answer on your own computer. WRITE CODE ON YOUR OWN SYSTEM.

The === operator is called a strict comparison operator, it does differ from the == operator.

For a==b to be true, a and b need to be the same value

In the case of a===b, a and b must be the same value and also the same type for it to be evaluated true.

For example :

Console output is true and false. So, using == operator might evaluate to true in a situation where you may not want it to be so using === operator is safer in many cases.


Another way of thinking is how 2 == ”2”? or how does this operator work?

We should know that == is equality with type coercion which means javascript will convert any necessary type conversions. So, == operator will compare for equality after doing the coercion/conversion (in this case to string)

So, here we came to know that === operator returns true if both value and type are the same.
Below, I have put down two interesting types of operands and cases for the === operator to return true. I have put one for you to code.

For reference types:

a === b returns true if a and b reference the exact same object

While for value types(numbers) : 

a === b returns true if a and b have the same value and are of the same type.

Homework: Get your own conclusion for the string.

Remember, you can’t learn to code unless you code by yourself. Many programmers ask me the IDE that I use. I use Visual Studio Code and I have been using Sublime Text for a long time. I love VS Code but that does not mean I don’t like sublime. I love sublime for it’s quick and light nature.

So, always code and fulfill the mission of this page i.e I CODE EVERY DAY – every day is code day. 

Likewise, after the questions on the equality operator, another question on the inequality operator is obvious, which is :

 What about

!= is an inequality operator that returns true only if the operator is not equal i.e coercion is done if needed. If two operand is of different data types, necessary type conversion is done. For the object operand, internal references are compared. So, this will return true if two references refer to different memory spaces.

 It will return false 

And, for the strict inequality or non-identity operator, the operator returns true only if the operator is not equal and not of the same types or any of two conditions.
i.e :

It will return true

There are a lot more to discuss on this since this is always a topic of discussion in every forum. For me, I always try to use === because I don’t like the unpredictable nature of == operator.

More and best is coming, So keep hope and stay alert.

Happy learning and Happy Coding.
You will learn more if you practice more.

Stay safe and Take care. Help others in need.


Related posts

Leave a Reply