Why you shouldn't use Double Equals Operator in JavaScript
Unlike many other programming languages JavaScript has both double equals '==' and triple equals '===' comparison operator. They might seem to do the same exact thing, but there is one major difference between the two that makes triple equals almost always better.
What's the Difference?
The '==' Operator
This is called Loose equality comparison operator in JavaScript.
Let's take an example here when comparing a string and an integer with the same value.
let a=1;
let b='1';
console.log(a==b);
OUTPUT
true
or wait let's try this
let a=1;
let b=true;
console.log(a==b);
OUTPUT
true
WHAT?? How is it true??
You may be wondering how this is possible? well because the '==' operator takes only value into consideration, not the type. Therefore matches boolean true to 1 by converting one of them.
The '===' Operator
This is called Strict equality comparison operator in JavaScript.
let a=1;
let b='1';
let c=true;
console.log(a===b);
console.log(a===c);
false
false
This returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 1 with '1' using ===, then it will return a false value.
When to use the '==' Operator
I don't usually recommend to use this often but you can use this when
- Checking if a value is null/undefined
- When you know both the operators of the same type
To Summarize
'==' Operator is Value based comparison
'===' Operator is Type+Value based comparison
So most of the times you should be using triple equals insead of double equals as it's more precise, accurate and doesn't bring up bugs which are not easy to find.
I love speaking to new people on the internet. Hit me up on Twitter or Linkedin , Lets talk tech.
Co-Founder, Hashnode
Thanks for the tip. I recommend using === almost every time unless there is a compelling reason not to. == can introduce unexpected bugs that are hard to find and fix.
Full Stack Developer
I really liked the way you have used images.
Awesome stuff!
Front-end developer, UI/UX designer
Good and easy to read article, thank you!
In section "The '===' Operator" the result of :
let a=1;
let b='1';
let c=true;
console.log(a===b);
console.log(a==c);
the output on the console wouldn't be:
false
true
?
Very good article, thank you.
Software developer, JavaScript Euthusiasts, Inspiring Blogger
The title of your post does not match your conclusion. The reason people tend to use triple equals is that they are not entirely sure what they are comparing. However, if you know both operands is a certain type, double equals are fine.
If you would tell me to write a web application from scratch, I would use double equals, the reason is to force people to write deterministic code.
Determinism in terms of the types you are comparing.
I would not argue any further, it a preference, a choice you make.
Think about it. If you know both types are the same, putting an extra =
to make ===
, isn't redundant.
Anyway, those things are agreed upon in a coding convention done by your team. If you usually use ===
, it is okay.
Sometimes, the type of data which are retrieved from apis is bitter vague, such as { code: 2, name: 'fengxh', type: 'human' } or { code: '2', type: 'clothes' }, from two different apis. So more codes to directly use ===
, unless a workaround +code === 2
. But I still recommend using ===
cada fuerte en algún moemnto fue debil
la verdad es algo que es tan basico que se aborda en la documentación y lo dejamos pasar. Que buen aclaración.
Comments (24)