My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

JavaScript Double Equals Sign Vs Triple Equals Sign

Ruchi Goswami's photo
Ruchi Goswami
·Jan 13, 2022·

2 min read

While learning JavaScript, double equals sign (==) and tripe equals sign (===) is encountered in the initial phase and therefore, it is important to know the difference between these two.

They are both value-comparison operations. Which one to use depends on what sort of comparison is required.

To start with, double equals (==) compares values of two things while triple equals (===) compares values and data types of two things.

Double Equals (==)

  • Will compare the values of two things
  • A type conversion will be performed, i.e, "1" - string, 1 = number
  • Also know as Abstract Equality, Loose Equality

Triple Equals (===)

  • Will compare the values AND data types of two things
  • Does NOT perform type coercion
  • Also know as Strict Equality , Identity

Let's see an example

const number = 9876
const string = '9876' 

console.log(number == string)     // true
console.log(number === string)  // false

The value of both number and string is same. However, the type of number is Number and type of string is String. Therefore, == check returns true while === check returns false.

When to use what?

When in doubt, I use '===' always. This might save a lot of time debugging the code later.

If we are supporting a particular use case where we can be a little relaxed about the data types, then use '=='. For example, if we should allow user input as 'true' or true.