Loose Equality(==) and Strict Equality(===) operators in JavaScript

Introduction

It is essential to know how the equality process works in JavaScript to create a reliable and predictable script. Two basic operators used for comparing values are

  • Loose Equality (==)

  • Strict Equality (===)

While most developers might mistake both as similar, there are distinct differences, especially when dealing with type conversion or coercion. Understanding their differences enables developers to make conscious decisions regarding the code they write, thus avoiding oddities like certain bugs and making the code more understandable.

Type coercion and type conversion

Type coercion is the process by which JavaScript automatically changes the type into another type required for the operation, resulting in some odd consequences like concatenation string operations (for example, “5” + 2 = 52). Type conversion, on the other hand, is a deliberate and direct conversion initiated by the programmer using objects such as Number(), String(), or Boolean(). Therefore, the programmer can easily predetermine how the data type will be handled. Both are important to identify when working with data management in JavaScript. That was just the tip of the iceberg, so let’s examine what each operator does depending on the data types involved.

Loose Equality (==)

The == operator does a type conversion of the two values to be compared. If the values differ, the JavaScript will try to convert one or both values to the desired type before the comparison. It is also referred to as type coercion. Examples are below

Different Types, Same Value:

let num = 10 == '10';
console.log(num); // output: true, because '10' is converted to a number before comparison

Here, JavaScript converts the string '10' to a number, so it essentially becomes 10 == 10.

Null and Undefined:

null == undefined; // true

In JavaScript, null and undefined are roughly the same thing, that is, == consider them equal

Booleans and Numbers:

true == 1; // true, because true is converted to 1 before comparison
false == 0; // true, because false is converted to 0

Special Cases:

  • Empty string comparison:

      '' == 0; // true, because '' is converted to 0
    
  • Arrays and strings:

      [1] == '1'; // true, because [1] is converted to '1'
    

When to Use Loose Equality

  • Use == only if you’re certain about the types of the operands or if you expressly prefer type coercion in JavaScript.

  • It is particularly helpful when you type check and are unsure whether a variable is null or undefined.

Strict Equality (===)

The === operator, known as strict equality, compares two values for their value and type; the operands will not be converted into a different type. There are more advantages to using this method than the former one in JavaScript: This kind of comparison is safer and more predictable since it ensures that the operands are the same type. Examples are below

Different Types:

10 === '10'; // false, because the types are different (number and string)

There is no type conversion here. The answer here is that 10 (a number) is not the same type as the value of '10' (a string), which leads the comparison to return false.

Same Type and Value:

10 === 10; // true, both are numbers and have the same value
'10' === '10'; // true, both are strings and have the same value

Boolean Values:

true === 1; // false, because true is a boolean and 1 is a number
false === 0; // false, for the same reason

Null and Undefined:

null === undefined; // false, because they are different types

Special Cases:

  • Objects are only strictly equal if they reference the same object in memory.

      let a = {};
      let b = {};
      a === b; // false, because they are two different objects in memory
      let c = a;
      a === c; // true, because c points to the same object as a
    

When to Use Strict Equality

Since it avoids type coercion, it's generally the preferred choice in most cases.

  • Use === when you need to ensure that the type of the value being set also matches this type.

  • It is specifically useful whenever you want to prevent bugs that originate from unexpected type conversion.

Key Differences Between == and ===

features== (Loose Equality)=== (Strict Equality)
Typeperforms type coercionperforms type conversion
Flexibilityit can compare different types, which makes it more flexibleit is less flexible; only compare the same types
ReliabilityThis may lead to unexpected results due to coercionMore predictable, recommended for most cases
performanceSlightly slowerfaster

conclusion

  • == (Loose Equality): Compares values after type conversion. It is useful in situations where flexibility is desired.

  • === (Strict Equality): It also compares both type and value without conversion. Most comparisons use it as generally safer and more reliable, so it is the preferred operator.

These differences let you write cleaner, less error-prone code and prevent JavaScript from jumping on you by incorrectly calling type coercion.

Thank you!