Loose Equality Vs Strict Equality in JavaScript

Loose Equality Vs Strict Equality in JavaScript

What are '==' and '===' in JavaScript ?

In Javascript, to compare two values, we use comparison operators. There exists a special case of comparison where two values are compared and decided whether the values are equal (or unequal). In that case, we can use the following two operators in javascript:

  • \== Operator.

  • \=== Operator.

What is "==" ?

The == operator checks if two values are equal. It is known as the loose equality operator. The == operator returns true if both operands are of the same data type and have the same value or if both are of different data types, but either of them can be converted to the data type of the other operand and have the same value. If both operands have different values, then it returns false.

Note: The == operator does type conversion of elements before comparing them.

What is type conversion?

The == and != operators loosely compare two operands, i.e., while comparing two operands, if both operands aren't of the same data type, then the operator tends to convert either of them into another operand's type and then compares their values.

Example:

Here we will compare two values, one string, and another number, then it will convert the string value to the number type and then compare the values.

let a = 10;
let b = '10';

console.log(a == b);

If both are of number data type, then the "==" operator will return true if both hold the same value.

let a = 10;
let b = 10;
let c = -10;

console.log(a==b);
//output: true

console.log(a==c);
//output: false

If both operands are of the string type, then the "==" operator will return true only if each element of the first operand matches with each element of the second operand.

let str1 = 'Javascript';
    let str2 = 'Javascript';
    let str3 = 'JavaScript';

    console.log(str1 == str2);
    //output: true

    console.log(str1 == str3);
    //output: false

If we are comparing two objects, then the operator will check if both refer to the same object. If yes, then the "==" operator will return true, otherwise, it will return false.

let car1 = {
    name: "Maruti"
}

let car2 = {
    name: "Maruti"
}

console.log(car1 == car1);
//output: true

console.log(car1 == car2);
//output: false

What is "===" ?

The === operator is called the strict equality operator. The === operators follow the Strictly equality comparison algorithm, i.e., it doesn't do the type conversion of the operands before comparing their values and returns false even if the data type of the operands aren't the same.

The === operator compares operands and returns true if both operands are of the same data type and have some value, otherwise, it returns false.

Example:

let a = 10;
let b = '10';
let c = 10;

console.log(a===b);
//output: false;

console.log(a===c);
//output: true;

Here the first output is false as a is a number type whereas b is a string type, the second output is true as both a and c have the same data type and value.

When both operands are objects.

let car1 = {
    name: "Audi"
}

let car2 = {
    name: "Audi"
}

console.log(car1 === car1);
//output: true

console.log(car1 === car2);
//output: false

Comparison and Difference Between == and === in Javascript

S. no\==\===
1Compares two operandsCompares two operands
2returns true if operands have the same data type and same value, and returns false if the values differ.returns true only if operands are of the same data type and same value, otherwise returns false
3In case both operands are of different data types, it performs type conversion of one operand to make the data types of the operands the same.In case both operands are of different data types, it doesn't perform type conversion of the operands.
4Also known as loose equalityAlso known as strict equality
5Follows abstract equality comparison algorithmFollows strict equality comparison algorithm