When you start to learn how to program in JavaScript, you might see code that uses three different operators that look very similar.
The = (equals), == (double equals), and === (triple equals) operators are all important in JavaScript, and they all do different things. Let’s look at the difference between the JavaScript symbols =, ==, and ===.
When to Use = in JavaScript?
The Javascript operator equal (=) is very different from double equals (==) and triple equals (===). This is what is called an “assignment operator,” and it is used to give values to variables.
var primary = "Hello";
In this case, we used the = (equal) operator to give the variable primary the value “Hello.”
When to Use == in JavaScript?
Both the double equal (==) operator and the triple equal (===) operator are comparison operators in JavaScript.
The double equal (==) operator, on the other hand, is used to find abstract equality. This means that when double equal (==) is used, it first changes the types of the two values and then compares them.
The following example will evaluate to true because the double equal operator will do something called “Type Coercion” to convert both values to the same type and then compare them.
if (100 == '100') {
//This will be equal
}
Your program will tell you when to use this operator. If you think that changing types will break the code, you might want to use the === operator.
When to Use === in JavaScript?
In JavaScript, the Triple Equal (===) operator is a strict comparison operator. This operator is used to compare two values while taking into account their types. If you used the first example, you’d get the opposite result.
if (100 === '100') {
//Not considered equal
}
Now you know the difference between JavaScript operators “=”, “==”, and “===”. Happy coding!