What you need to try the examples

These examples use JavaScript. You need a browser with a developer console that supports ECMAScript 2015 (ES2015) or later; no packages or project setup are required. Open the console, type an example, and press Enter. You can also place the examples in a JavaScript file and run them with a compatible JavaScript runtime.

The two operators answer different questions. The assignment operator, =, means “put this value here.” The strict equality operator, ===, asks “are these values equal without converting one type to another?” Remembering that difference makes conditions and variable updates much easier to read.

Use = to assign a value

Assignment gives a variable a value or replaces the value it already holds. For example, let score = 0 creates a variable named score and assigns it the number 0. Later, score = 5 changes that variable’s value to 5. Typing score into the console after those statements shows 5.

A variable declared with const cannot be assigned a new value. For example, const limit = 10 followed by limit = 12 produces an error. Use let when the variable must be reassigned, and const when you do not intend to reassign it. The operator = does not ask whether the old and new values match; it performs the assignment.

Assignment is also an expression that produces the assigned value. That detail matters in conditions: if (score = 5) assigns 5 to score, then tests the resulting value. Because 5 is truthy, the condition passes. JavaScript allows this syntax, but it is usually a mistake when you meant to compare values.

Use === to compare values

Strict equality compares two values and produces either true or false. It does not change either value. For example, after let score = 5, the expression score === 5 produces true, while score === 6 produces false. Read score === 5 as “is score strictly equal to 5?”

Type matters. The expression 5 === "5" produces false because one value is a number and the other is a string. By contrast, 5 === 5 and "5" === "5" both produce true. If a form or prompt gives you text that represents a number, decide whether you want to compare it as text or convert it to a number first. Do not assume that values which look alike have the same type.

JavaScript also has ==, called loose equality, which can convert types during comparison. For example, 5 == "5" produces true. Beginners can usually make their intent clearer by starting with === and handling any needed conversion explicitly.

Spot the difference inside an if statement

Suppose a program should show a message only when a user has three attempts left. The intended condition is if (attempts === 3) { console.log("Three attempts left"); }. This checks attempts and leaves its value unchanged.

Now consider if (attempts = 3) { console.log("Three attempts left"); }. This assigns 3 to attempts. The condition then receives 3, which is truthy, so the message appears even if attempts previously held a different number. The bug is not that JavaScript confused the operators; it followed the assignment exactly as written.

If you are unsure what a condition does, break it into small console checks. Try let attempts = 1; then evaluate attempts === 3 and inspect attempts. Next, evaluate attempts = 3 and inspect attempts again. You will see a false comparison in the first case and a changed variable in the second.

A useful rule of thumb, with one important limit

When writing a condition, ask whether you intend to change a value or check it. If you mean to check, use ===. When setting or updating a variable, use =. Descriptive variable names and a quick reread of each if condition can help you catch an accidental assignment.

Strict equality is not a deep comparison of objects. For example, { name: "Ada" } === { name: "Ada" } is false: those are two separate objects, even though their contents look the same. If two variables refer to the very same object, comparing those variables with === produces true. For now, the key distinction is simple: = stores a value; === returns a true-or-false comparison.

Back to category ↗