JavaScript – Double Equals Vs Triple Equals

JavaScript - Double Equals Vs Triple Equals

JavaScript provides two different operators to test the equality of two values or variables. These operators are the double equals (==) and the triple equals (===).  Both these operators are used to test if the two values under consideration are equal. However, there is a key difference between the two.

See Also: 10 JavaScript Console Tricks You Probably Didn’t Know

The double equal operator tests only if the two values or variables which are being compared are equal and will return true even when the type of these values are different. To understand this in detail consider the following example. As you can see, the types of two values are different one is a number and the other is a string.

if (8 == '8') {
  console.log("I'm in!")

In the above example, if statement will return true and the string will be logged in the console.

Unlike the double equals operator, the triple equals operator not only tests if the two values are equal but also check if the types of the values under consideration are also equal.

if (8 === '8') {
  console.log("I'm in!")

In the above code, we’re using triple equals for comparing the two values the if statement will return false and no output will be logged on screen. 

Whenever possible, it is recommended to use triple equals operator to compare to values as it compares both values as well as types.

Thank you for reading! I hope you found it useful. In case, you’ve any query of feedback, feel free to drop a comment.

One thought on “JavaScript – Double Equals Vs Triple Equals”

Leave a Reply

Your email address will not be published. Required fields are marked *