Deep Tech Point
first stop in your tech adventure

Javascript === operator vs ==

February 12, 2021 | Javascript

In Javascript we have a couple of options for checking equality, so let’s take a look at them – what are similarities, and what are the differences.

What is == in Javascript?

Double equal operator or == is also known as the equality or abstract comparison operator. This operator transforms the operands having the same type before comparison. You can use the equality operator to check the identity of two operands even though they are not of a similar type. And this actually is one of the most important uses of equality operator – to check whether the two operands are the same or not by changing an expression from one data type to the other.

Double equal operator or == will always convert the variable values to the same type before it will perform a comparison. This is called type coercion.

So, for example, if you decide to compare a string with a number, Javascript will convert any string to a number. Javascript will always change an empty string to a zero. However, a string with no numeric value will be converted to NaN (Not a Number), and this will return false.

What is === in Javascript?

In Javascript === or triple equal operator is also known as the identity or strict comparison operator. Comparing to the equality operator, the identity operator (===) does not perform any type of coercion – it does not implicitly convert the variable values to the same type before it performs a comparison. Instead, === performs type casting for equality which means that the operator will return true only if both values and types for the two variables that are compared are the same or identical. So, if the values are not of a similar type, the strict comparison operator will return false – for example, if you compare 5 with “5” using ===, it will return false, because it will check that the two values are of different types, therefore the values are considered as unequal.

Variables are considered as equal (true), if they are of the same type, and have the same value, and are not numeric. However, if both variable values are numeric, === in Javascript will recognize them as equal, if both are not NaN (Not a Number) and are the same value.

So, what are the major differences between == and === in Javascript?

You can use == to compare two variables, but in the process, you will ignore the data type of variables. (Type coercion)
If you decide to use ===, you will also compare two variables, but this operator will not ignore the data type – it will check the data types and then compare the two values. (Type conversion)

So the main message to take home is the following. The == will compare the equality of two operands, but it will ignore their type, therefore it will return true if the two operands are equal and false if the two operands are not equal. This operator will first check the equality only after doing the necessary conversions.

The === will check the equality of two operands with their types- === takes a type of variable into consideration, therefore the === will return true only if both values and data types are the same for both variables. However, if the two variable values are not of the same type the === will not perform any conversion.