Why you shouldn't use Double Equals Operator in JavaScript

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

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'; 



or wait let's try this

let a=1;
let b=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;

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.


Interested in reading more such articles from Hrithwik Bharadwaj?

Support the author by donating an amount of your choice.

Recent sponsors
Prateek Aher's photo

I like the cover image you used Hrithwik.

Sandeep Panda's photo

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.

Peter Thaleikis's photo

Pretty much sums it up 👍️

Prateek Gogia's photo

I really liked the way you have used images.

Awesome stuff!

Peter Thaleikis's photo

Thought the same :D

Karina Khodzhakulieva's photo

Good and easy to read article, thank you!

double's photo

== or riot

Julian's photo

Let's do a riot

Triple's photo

u wanna fight hu

fengxh's photo

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

Dinys Monvoisin's photo

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.

Show +4 replies
Mark's photo

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.

Dinys Monvoisin's photo

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.

Iverson Pereira's photo

In section "The '===' Operator" the result of :

let a=1;
let b='1'; 
let c=true;

the output on the console wouldn't be:



Very good article, thank you.

Hrithwik Bharadwaj's photo

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

Ikeh Leonard's photo

Good and simple read

Juan David's photo

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