Skip to main content

Command Palette

Search for a command to run...

Why you shouldn't use Double Equals Operator in JavaScript

Updated
2 min read
Why you shouldn't use Double Equals Operator in JavaScript
H
I am a full stack developer from Mysore India. I am here to share as much information I know with the world in the most articulate way.

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

https://cdn.hashnode.com/res/hashnode/image/upload/v1599030905638/ChglLb-z-.jpeg

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.

hrithwik.jpg

Comments (10)

Join the discussion
J

la verdad es algo que es tan basico que se aborda en la documentación y lo dejamos pasar. Que buen aclaración.

I

Good and simple read

F
fengxh5y ago

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 ===

1
I

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.

1
H

Oops thanks for pointing out . I wanted to use triple equal in both the ones

D

Good and easy to read article, thank you!

2
P

I really liked the way you have used images.

Awesome stuff!

4
H

Thanks mate

P

Thought the same :D

D
double5y ago

== or riot

2
J
Julian5y ago

Let's do a riot

T
Triple5y ago

u wanna fight hu

1
P

I like the cover image you used Hrithwik.

11
H

Haha, Thanks Prateek Aher

1
D

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.

1
H

Yeah everyone has their own reasons but most of the time people should use '===' unless someone wants to check if a variable is null or undefined. Any suggestion for the conclusions ?

1
D

I may be biased, but before I was using only triple equals. Now I prefer double equals.

For the conclusion, I would say it depends on the level of experience of the team of developers. If your team consist of most junior developers and you do not have proper standard for checking of types, stick with triple equals. Simple and reliable.

T

Dinys Monvoisin,

Interesting thought. Do you recommend Object.is usage as it is more focused on the similarity than equality? It seems, Object.is would be more safeguarding with the experience level you were mentioning.

=== and Object.is are having lesser gotchas than the == anyways.

D

Tapas Adhikary,

If I am not mistaken Object.is one of the edge cases is -0. There are only a few such cases. Therefore, I prefer to stick with == as it is less verbose and only recommend using Object.is only for those.

However, many people do not know the various coercions of ==, but most of the obscure one like "" == false should at first hand never see the day in the application you develop.

M
Mark5y ago

What do you mean by deterministic code? The way I understand determinism, it's unrelated to ==.

I would agree it's better to know the types of things you're comparing. But it doesn't hurt to use === to prevent surprises, right? Experienced developers are not immune to making mistakes.

D

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.

S

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.

8
P

Pretty much sums it up 👍️

More from this blog

H

Hrithwik Bharadwaj

20 posts

I write things about my Side projects and JavaScript