JavaScript === vs ==

Harshit Jindal Oct 12, 2023
  1. Loose Equality Operator (==) in JavaScript
  2. Strict Equality Operator (===) in JavaScript
JavaScript === vs ==

Comparison operators help us to determine whether two variables are equal or follow a specified criterion. If the comparison results are favorable, it returns true; otherwise, it returns false. This tutorial explains the most confusing topic among comparison operators and teaches when to use which equals operator(== or ===).

A single equals = is quite different from double equals == and triple equals === because it is an assignment operation and they are comparison operators. Therefore, the confusion is generally between == and ===.

Loose Equality Operator (==) in JavaScript

The == operator or equality operator is also called the abstract comparison operator. It is called abstract because it is only concerned with the value and not the type of the variable. The == operator performs the conversion of variable values to the same type (type coercion) before comparing them with each other and returns true if the converted operands are equal. Since the operator has to perform type conversion, it tends to be a little slower than the === operator.

Now, to understand when to use which operator, we first have to understand a little about type coercion. It is of two types:

  1. Explicit Coercion: It is done explicitly through code using built-in methods. For example: To convert a string "42" to a number, we have to write Number("42"). This way, we use the explicit Number() type conversion method to convert a string to a number.
  2. Implicit Coercion: It is done implicitly by the language. It is usually performed when we use two different types of the operand with an operator. For example: If we perform 1 + "". There is one number and one string that can not be added directly, so JavaScript type converts the number to string as it’s the only way they can be added and returns a string "1".
23 == '23'         // returns true
true + false == 1  // returns true
undefined ==
    null         // returns true
        [] == 0  // returns true

Strict Equality Operator (===) in JavaScript

The === operator or identity operator is also called the strict comparison operator. It is called strict because it returns true only when both the type and the value of both operands are the same. It does not perform any type of conversion; hence, it tends to be faster than the == operator.

true === true  // returns true
true === 1     // returns false

Hence, the decision of choosing the == or === operator lies in need for type coercion. If one is not familiar with type coercion, then he should stick with strict equality operator.

Harshit Jindal avatar Harshit Jindal avatar

Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.

LinkedIn

Related Article - JavaScript Operator