Introduction
As we know, when we want to check for two equal values in JavaScript we use an "if"statement with a double equals operator (==).
- If(a == b)
- {
- //Do something;
- }
- function Func1()
- {
- /Write some logic
- return 123;
- }
- function Func2()
- {
- //Write some logic
- return ‘123’;
- }
I checked the value of both functions using an if statement with a double equals operator and surprisingly it returned true.
Example
Output: Both are equal
After research, I came to understand that the condition using the Double-Equals Operator was checking for values after converting the value to a specific type by the browser. As you all know, there are various types available in JavaScript like Number, Boolean, String, and Objects. If you do a comparison between numbers in string format (for example "123") with the actual integer value (for example 123) then the browser will convert the number in string format to the actual integer before comparison. And the output will be "Both are equal". How to solve this?
- if (Func() == Func())
- {
- alert(‘Both are equal’);
- }
- else
- {
- alert(‘Both are different’);
- }
Then I came across with Triple Equal Operator (===) that is also called as strict equals
This operator checks for actual values without converting the type before comparison. So if we consider the same example then the output will be "Both are different". So I would recommend you to use Triple Equals Operator (===) instead of Double Equals Operator (==) since there would be an actual value comparison instead of forcefully trying and converting the type and doing the comparison.
Example
Output: Both are different
The same rule applies to the not equal operator (!=) too. It will try to convert the value before comparison.
And if you use (!==) then type conversion is skipped and the value is actually checked for the != condition.
Some more surprise results which I observed in JavaScript are given below:
if (1 == true) //Output: true
if('' == 0) //Output: true
- if (Func() === Func())
- {
- alert(‘Both are equal’);
- }
- else
- {
- alert(‘Both are different’);
- }
Where as:
if (1 === true) //Output: false
if('' === 0) //Output: false
Conclusion
The use of the Triple Operator is more beneficial than using the Double Operator when comparing two values.

Ashish SrivastavaPosted Jan 5, 2017, 10:05 AM
Fantastic article for developers
Sukesh MarlaPosted Jun 22, 2014, 6:16 AM
nice
Gopi ChandPosted May 22, 2014, 10:48 AM
ok..i'will take care of it...thanks..
Pawan ChandPosted May 22, 2014, 4:19 AM
very helpful article
Pradeep ShetPosted May 21, 2014, 12:52 PM
Thanks Anupam
Anupam SinghPosted May 20, 2014, 3:04 AM
very informative ..
Gopi ChandPosted May 19, 2014, 2:24 PM
Yes using double operator will be more optimistic..
Sam HobbsPosted May 19, 2014, 1:02 PM
Sometimes the use of the Double Operator is more beneficial than using the Triple Operator.