What's the Difference Between the Assignment (`=`) and Equality (`==`, `===`) Operators?
Naveera A.
—People who are new to programming often find it difficult to understand the difference between =
, ==
, and ===
.
In mathematics we only use =
, so what do the other two mean?
The =
is an assignment operator, while ==
and ===
are called equality operators.
=
)In mathematics and algebra, =
is an equal to operator. In programming =
is an assignment operator, which means that it assigns a value to a variable.
For example, the following code will store a value of 5
in the variable x
:
let x; // declaring a variable. x = 5; // assigning a value of 5 to the variable.
We can combine the declaration and assignment in one line:
let y = 10;
It may look like the assignment operator works the same way as algebra’s equal to operator, but that’s not the case.
For example, the following doesn’t make any sense in algebra:
x = x + 4;
But it is acceptable in JavaScript (and other programming languages). JavaScript will take the expression on the right-hand side of the operator x + 4
and store this value in x
again.
==
)In JavaScript, the operator that compares two values is written like this: ==
. It is called an equality operator. The equality operator is one of the many comparison operators in JavaScript that are used in logical and conditional statements.
The equality operator returns true
or false
based on whether the operands (the values being compared) are equal.
For example, the following code will return false
:
let x = 5; // declares x and assigns x to the value 5 x == 8; // returns false
Interestingly, if we compare an integer 5
and a string "5"
it returns true
.
5 == "5"; // returns true
That is because in most cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for comparison. This behavior generally results in comparing the operands numerically.
===
)Like the equality operator above, the strict equality operator compares the two values. But unlike the equality operator, the strict equality operator compares both the content and the type of the operands.
So using the strict equality operator, 5
and "5"
are not equal.
5 === "5"; // returns false
It is better to use the strict equality operator to prevent type conversions, which may result in unexpected bugs. But if you’re certain the types on both sides will be the same, there is no problem with using the shorter operator.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.