Is there any difference between = , == , ===?
Table of contents
Yes, there is a lot of difference between these three operators.
First, we discuss the = (equal to) operator. In general, we say this is equal to operator it means the left-hand side value is equal to the right-hand side value as well as right-hand side value is equal to the left-hand side value but in programming, this is not called equal to operator. This operator is called the assignment operator. The work or this operator is to assign a value to a variable. Let's take an example
in this example value 10 is assign to a variable name a.
const a = 10;
console.log(a); // Print 10
console.log(10); // Print 10
The first console log will print 10 because the value of a is 10. But we all know equal to mean L.H.S. = R.H.S. but we see that in programming we call this assingment operator. This operator works to assign value to a variable from the right-hand side to the left-hand side. It never assigns a value from the right-hand side to the left-hand side. So that's why the second console log will print 10 not a.
After understanding this, your question is maybe if we have to compare two values then how will we compare because if we use an assignment operator it will assign a value from left to the right-hand side. So that's why the == operator comes in scenario.
The Comparison Operator
Yes == is called Comparison Operator. It is used to compare two value if both values is equal then this returns true otherwise false.
const a = 10;
const b = 10;
const c = 20;
const d = '10';
console.log(a == b ); // True
console.log(a == c); // False
console.log(a == d); // True
In the first console.log, we compare two variables a and b and both the variable have the same value so they print true, and in the second console.log they print false because variable a is not equal to c but in the third console.log variable a is an integer and variable d is a character so why they print True because comparison operator will check the value, not the datatypes. So, when you compare a string with a number, JavaScript converts any string to a number. An empty string is always converted to zero. A string with no numeric value is converted to NaN (Not a Number), which returns false.
Strictly Equality Comparison Operator
As we see the comparison operator is not the datatypes of the variable. So to compare the value and to check the datatype of the variable we use Strictly Equality Comparison Operator (===). This operator performs type casting for equality.
const a = 10;
const b = '10';
console.log(a === b); // False
In the previous example console.log prints False because a is an integer and b is a character.
Hope you understand the difference between these three operators(=, ==, ===).
If you want to know more about this operator read mdn docs for a better and deeper understanding.