
Why Does the “==” Exist in JavaScript?
When I first switched from Python to JavaScript, it was like moving to a house where they decided to have two different light switches for the same light. You know, just to keep things interesting.
In Python, ==
just worked. Here, we have ===
, and then there’s that other thing, ==
. So, why does the double equals operator even exist?
A Bit of a Backstory
A little-known fact is that JavaScript was built in May 1995 by Brendan Eich in just ten days. Initially, it was called Mocha, then LiveScript, and it is now known as JavaScript.
JavaScript was thrown together really fast to make websites more interactive. In that rush, they made ==
to be easy to use, especially for beginners. It tries to make comparisons simpler by changing the types of values to match each other.
A Quick Look at What ==
Was Made For
"1" == 1 // true (JavaScript changes the string "1" to the number 1)
0 == false // true (JavaScript changes the boolean false to the number 0)
null == undefined // true
At the time, these design choices seemed sensible, but the shift in programming practices exposed the flaws in the ==
operator’s core concept.
The problem is that changing types can lead to very strange results. For example:
"0" == false // true
"" == 0 // true
" \t\r\n" == 0 // true
The Introduction of ===
So about four years later, in December 1999, the strict equality operator ===
was introduced in JavaScript.
Strict equality (===
) is the current standard, but you can still find loose equality (==
) in legacy code or when people are trying to be clever. For example:
-
Checking for null or undefined:
Some people usevariable == null
. Butvariable === null || variable === undefined
is much clearer. -
Older programs:
Legacy code might still use it.
It Is Time to Retire the ==
Operator
It would be great if JavaScript got rid of ==
altogether. It causes more problems than it solves. Or, perhaps a better solution would be to make ==
and ===
do the exact same thing in future versions of JavaScript.
This would eliminate the confusion and make JavaScript more consistent. At the very least, a warning when someone uses it would be beneficial.
Conclusion
In conclusion, use ===
. ==
is from the past. Use ===
for comparisons. It's simpler and more reliable.
So, there you have it — the story of ==
and why ===
is the better choice. What are your thoughts? Let’s discuss it in the comments. Also, if you found this article insightful, consider giving it a clap. And a follow would be awesome!