The equal (=), double equal (==), and triple equal (===) signs, which are respectively known as the assignment, equality, and strict equality operators in JavaScript, often confuse junior JavaScript developers. Inappropriate use of these operators can make programs run abnormally, sometimes without the compiler throwing an error, making codes difficult to debug.
In this article, I will explain what each of them does and how and when to use them.
Lets dive in!
The Assignment Operator (=)
The assignment operator is the same across all programming languages. As its name implies, in the simplest form, it is used to assign values to variables. The variable is on the left-hand side and the assigned value is on the right-hand side.
It can also be combined with the arithmetic operators to perform simple operations before assignment. For more on this, click here.
The above image illustrates the usage of the assignment operator and its shorthand forms.
It was coded as follows:
Line 1 declared a variable called
newVariable
.Line 2 assigned the value 20 to
newVariable
.Line 36 syntax is
newVariable = newVariable <op> 10
. Where<op>
is replaced with the appropriate operator, e.g., line 3 is the same asnewVariable = newVariable + 10
.
The assignment operator has right-to-left associativity, that is, operations are performed from right to left. Thus, line 3 of the code will add newVariable
to 10 before assigning the result back to newVariable
(itself).
The Equality Operator (==)
The equality operator, which has a double equal sign, is also similar across most programming languages.
The operator is used to compare two or more expressions and always evaluates to a true or false (boolean) value.
In the above code:
I assigned 100 to variable
num1
and 300 to variablenum2
, thennum1
andnum2
variables are compared and at the same time stored in a result variable.And a false value is logged to the console since the values of
num1
andnum2
are not equal.
Before comparison, the operator also performs type conversion if necessary.
The above code compares a variable string of 10 with a variable number of 10 using the equality operator, and a true value is obtained, though theyre of different types.
There is also an inequality operator (!=) in JavaScript which does the exact opposite of the equality (==) operator.
Changing the equality operator in the above code to an inequality operator, a truthy value is obtained.
The Strict Equality Operator (===)
The strict equality operator, unlike the other two operators, is only available in two programming languages: PHP and JavaScript.
The operator works like the equality operator, except that it does not make a type conversion before comparison.
In the above diagram, a comparison using the strict equality operator, though having a value of 10 of different types, evaluates to false.
Using the strict equality operator is considered as best practice to avoid code bugs.
The strict inequality operator (!==) in JavaScript does the exact opposite of the strict equality (===) operator.
The above code checks if string 50 is not equal to number 50, using the strict equality operator, and it evaluates to true.
Check here for more on the difference between equality and strict equality operators.
Conclusion
In this article, I explain the difference between the assignment, equality, and strict equality operators. You should now be able to distinguish between the three operators as well as their use cases.
Top comments (0)