What's the Difference Between the Assignment (`=`) and Equality (`==`, `===`) Operators?

Naveera A.

The Problem

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 Solution

The = is an assignment operator, while == and === are called equality operators.

Assignment Operator (=)

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.

Equality Operator (==)

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.

Strict Equality Operator (===)

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.

Get Started With Sentry

Get actionable, code-level insights to resolve JavaScript performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a JavaScript project and note your DSN

  3. Grab the Sentry JavaScript SDK

<script src="https://browser.sentry-cdn.com/7.111.0/bundle.min.js"></script>
  1. Configure your DSN
Sentry.init({ dsn: 'https://<key>@sentry.io/<project>' });

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.