"==" vs "===" in JavaScript ?

if you are struggling with "==" and "===" then read this article

"==" vs "===" in JavaScript ?

Introduction

Hello Reader, If you are a JavaScript Developer or want to become then I am sure that you have seen "==" and "===" in JS. If you are also confused that in all other Programming languages, we are using "==" for comparison then why we are using "===" in JavaScript? So let's find out about this concept:

"==" is called loose equality operator and "===" is called strict equality operator. What does it mean? Let's understand

Loose Equality (double equals)

loose equality operator compares two values without comparing their type. If they have the same value then it returns true even if one is a number and the other one is a string. '==' operator performs type coercion if necessary to make the comparison possible. Let's take an Example:

let a = 1; //number
let b = '1'; //string
a == b //true

We can understand it in a very simple manner. suppose we have 4 fruits in which 2 fruits are apples🍎🍎 and the other 2 are oranges 🟠🟠. if we apply the loose equal operator 🍎🍎 == 🟠🟠then it returns true because their quantity is the same. It can be useful in some places but not always. That's why JavaScript Provides an extra level of precaution.

Strict Equality (triple equals)

A strict equality operator compares two values but it compares their type also. If the values are the same but have different types, the values are considered unequal. '===' operator does not performs type coercion. Let's take an Example:

let a = 1; //number
let b = '1'; //string
a === b //false

let num1 = 1; //number
let num2 = 1; //number
num1 === num2; //true

Let's take the fruit example of apple and orange if we apply the strict equality operator 🍎🍎 === 🟠🟠 then it returns false because the quantity is the same but both the fruits are different.

Conclusion

If you read this blog carefully then I am sure that you have understood "==" and "===", where we are using them. Let's summarise all the things:

  1. "==" only checks for values.

  2. "===" checks for value as well as type.

Β