Are They Different? == vs ===

Are They Different? == vs ===

First, let's understand what those symbols represent. These symbols are used to check whether the value on both sides is the same or not if its same returns True or if don't match false. It basically compares both values. These symbols are used in conditional statements and logic where the program has to make the decision depending on the outcome. This operator is called Equality Operator / Comparison Operator.

They do have different names:

  1. Loose Equality Operator ( == )

  2. Strict Equality Operator ( === )

Let's find the difference:

A loose Equality Operator is a normal equality operator which checks whether the value on both sides is matching or not. But it doesn't care what type of value is compared with what type. If the value matches it returns true.

How?

it implicitly changes the value to the value type which it is compared with. Example listed below

let minimumAge = 34;
let saraAge = '34';

// Variable Example
if(minimumAge == saraAge){
// The code gets Executed
console.log('True');
}

// Direct Value 
if(34 == '34'){
console.log('True');
}

Here the minimumAge is an Integer value and saraAge is a string that represents the exact value as minimumAge but in the String form. So javascript implicitly converts the string value into number and returns TRUE.

Strict Equality is something that you need to use instead of Loose. This operator doesn't convert the value even if the value matches with the other value if even check for the type if both the types match then return TRUE or if they don't FALSE

let minimumAge = 34;
let saraAge = '34';

// Variable Example
if(minimumAge === saraAge){
// The code won't get Executed
console.log('True');
}

// Direct Value 
if(34 == '34'){
console.log('True');
}

Here this returns false because the minimumAge is an Integer value and saraAge is a string. It won't perform any type of conversion.