90

# How to use double equal and triple equal in JavaScript

Double equal and triple equal operator are very useful and common for making comparison when writing JavaScript.

Let’s move into knowing what are they and how they works!

## Double Equal “==”

Double equal or “==” is a comparison operator that will convert your values to common type before comparing them. This is also known as type coercion. Thus, this is also consider to be more of a loose comparison.

``````if ('123' == 123) {
console.log('true');
}
// true

if (123 == 123) {
console.log('true');
}
// true

if (false == 0) {
console.log('true');
}
// true``````

When compare an identical string to a number of the same value via a double equal, it return as a match.

The interesting part in JavaScript is that comparing false with 0 will return true. This is because Javascript have the concept of falsy values where there are 8 values considered false in a boolean environment. More info can be found here.

## Triple Equal “===”

Triple equal or “===” is a comparison operator in JavaScript that will compare the values with no conversion. Hence, it will not just do a value check but also the data-type check.

``````if ('123' === 123) {
console.log('true');
}
// nothing printed

if (123 === 123) {
console.log('true');
}
// true

if (false === false) {
console.log('true');
}
// true

if (false === 0) {
console.log('true');
}
// nothing printed``````

From the first example above, since the comparison is done between a string type and a number type, it will not pass the check. The following 2 examples match since they are both of the same type and the same values.

The last example will not be successful since false and 0 are of a different data type.

## Conclusion

Double equal is an abstract of Triple equal operator where double equal only check for the value equality but triple will also check for data-type equality.

As a good practice, it will always be a good practice to use triple equal operator for comparison to easily proof code functionality and improve readability. However, there may also be situation that requires to use double equal. So there is not really a right or wrong answer here.