Skip to main content

Command Palette

Search for a command to run...

== Vs === in JavaScript

Updated
β€’3 min readβ€’View as Markdown
== Vs === in JavaScript

Strict VS Loose Equality

Hi folks, today I am going to talk about the difference between "==" in comparison with "===" operators in JavaScript.
So these are both ways to check for equality. They allow you to check if something on the left is equal to something on the right. And the difference between the two is that the double equals do not care about the datatypes whereas the triple equals do. Here my point is that when we use Loose Equality it works a step ahead of Strict Equality by working on type conversion and then returning something.

The Loose Equality Operator

The loose equality operator β€˜==’ allows us to compare two or more operands by converting their value to a common type first and then checking for equality between them.

Example 1πŸ‘‡

Let's say that we have to check that the string "5" is equal to the number 5.

console.log('5' == 5); // true

So in the case above JavaScript works behind the scenes to convert the type of these two values and makes sure that these two are the same and once they are the same it again makes sure that the two values are equivalent to each other.

So, what I am implying is that the JavaScript engine is automatically converting the types for you behind the scenes.

Okay, let's try another example.

Example 2πŸ‘‡

console.log(" " == 0); // true

In the above example, we are trying to equate an empty string( " ") to 0.
and here the JavaScript engine considers an empty string to be equal to zero when using the double equal operator. It converts this behind-the-scenes to the same type and then it is comparing the values.

Now let's talk about the "===" operator or strict equality operator.

The Strict Equality Operator

The strict equality operator β€˜===’ allows us to compare two or more operands by checking the equality between the values as well as their types. It returns true only if the values and the type both match with the other operand

The strict equality operator takes the datatype into account.
Now let's go back to Example 1.

console.log('5' === 5); // false

In the example above the output would be false. This is because we are trying to equate two different things here. This '5' here is a string and this 5 is a number and since the string and number datatype do not match we would get a false output straight away.

Now if I was to compare it with the same data type it would give true.

console.log(5 === 5); // true

Important Points

Here we need to keep in mind two very important differences

1. First and most important is that the Loosely equal operator is going to convert the data type if you have numbers and strings it is going to try to convert that string to a number, if you have a number and a boolean it is going to convert that boolean to a number and so on.

  1. And the second one is that loosely equal is going to check for that undefined and null.

References

MDN | strict vs loose