Difference Between Double (==) and Triple (===) Equals in Javascript

What’s the difference between double and triple equals operators .  Javascript supports both supports strict equality (===) and type-converting equality (==).

In the beginning it’s seems confusing to me when i see comparison using triple equals. Later i know the reason why triple comparison is used.

In this post i am going to explain what’s the difference between double(==) and triple(===) equal operators.

Difference Between Double (==) And Triple (===) Equals in Javascript

Double (lenient or normal) equal to  compares the two value and return true or false based on that.

Double equality (==) converts the operands to the same type before making the comparison. That’s why it is also called type-converting equality.

So comparing number with string having the same value will return true.

How to Reverse String in Javascript.

For example –

It returns true because both value are same. Take another example.

In this comparison first value is string and second value is integer. It return true value, as it typecast the first value to int.

Triple Equals

Now let’s see how Triple equal (strict comparison) works in this case. Triple equality operator compares values as well as data type.

If you compare “8” with 8 using triple equals to the result will be false. As this “8” represent string values whereas other 8 represents integer.

Example of Double (==) and Triple (===)Equal in Javascript

Let’s check some example with double and triple equal to.

1.  When we compare two strings with triple equality (===), it return true when they have the same sequence of characters, same length, and same characters in corresponding positions.

2. Two numbers are strictly equal only when they are numerically equal.

Difference between single and double quotes .

3. Two Boolean operands are strictly equal if both are true or both are false.

4. Comparing Objects with double (==) and triple (===) equal.

When two objects are compare using triple equal (===), it return true if and only if they refer to the same instance of the class.

5. In Javascript if we compare Null and Undefined using double equal (==) then true is return. But when we compare Null and Undefined with triple equal to (===) it return false.

Difference between Null and Undefined value.

Performance comparison of Double and Triple equal

If you are comparing two variables of same data type, both the operator takes equal time. But if the values you are comparing are of different type in that case triple equals operator will be faster because it would not try to convert the types of variable.

Conclusion

If you need strict comparison always use triple equal (===) operator.

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.