Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What is the difference between `==` and `===` in JavaScript?

Quick answer

=== compares without converting operand types, so 0 === "0" is false. == may convert an operand before comparing, so 0 == "0" is true. The operators can therefore give different results for the same values. Use === by default for predictable comparisons; use == only when you deliberately want its conversion behavior and understand the comparison involved. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does the difference mean in practice? I have seen JavaScript return different results for 0 == "0" and 0 === "0", and I am not sure which one to use.

Geez

The difference is whether JavaScript converts operand types before comparing. With ===, it does not convert them; if the operands have different types, the result is false. With ==, JavaScript can convert an operand first. That is why 0 == "0" is true, while 0 === "0" is false. [S1]

A useful way to think about it: === asks whether the values compare equal without changing their types. == allows JavaScript to apply its conversion rules first. That can make a loose comparison less obvious when you read the code.

Follow-up question

Could you show the results for a few comparisons in one place, including one where both operators agree?

Geez

Here are representative comparisons:

  • 0 == "0" → true: loose equality can convert the string before comparing.
  • 0 === "0" → false: strict equality does not convert, and the operand types differ.
  • 4 == 4 → true; 4 === 4 → true: both operands are the same number, so the operators agree.

The first two examples demonstrate how a type difference can change the result. The last one shows that the operators do not always disagree; the difference matters when loose equality’s conversion behavior affects the comparison. [S1]

Follow-up question

I am checking a value before running a block of code. How should I decide which operator to put in the condition?

Geez

Use === by default when you want the comparison to respect the types of the values. For example, if value is expected to contain the number zero, value === 0 does not treat the string "0" as the same value. This makes the condition’s intent more explicit.

Choose == only when conversion is part of what you want. Before using it, identify the operands and consider whether JavaScript’s conversion rules produce the result you intend. Loose equality has special cases, so 0 == "0" is not a complete guide to every possible == comparison. [S1]

Follow-up question

Can you give me a reusable rule and a quick review checklist for code that already contains ==?

Geez

A practical rule is: write === unless you can explain why a particular == comparison should allow conversion. When reviewing a condition, identify its two operands and expected types. Then ask whether conversion could make the condition match values you did not intend.

For example, do not assume value == "0" checks specifically for a string: loose equality can convert an operand before comparison. If you need the comparison to distinguish the string from the number, use strict equality against the intended type. The point is to choose deliberately, not to assume every use of == is automatically wrong. [S1]

A practical checklist

  • Identify the two operands and their expected types.

  • Use === when you do not want type conversion.

  • If using ==, check that its conversion behavior matches your intent.

  • Compare a type-mismatch example, such as 0 and "0".

  • Make the condition’s intended behavior clear to the next reader.

A few more useful answers

Does `===` convert one operand to match the other?

No. Strict equality compares without type conversion and returns false when operand types differ. [S1]

Does `==` always give the same result as `===`?

No. Loose equality can convert operands before comparing them, as 0 == "0" shows. The operators can agree in some comparisons, but they are not interchangeable. [S1]

Is `==` forbidden in JavaScript?

No. It has conversion behavior and special cases. Prefer === for predictable comparisons, and use == only when you deliberately want the relevant loose-equality behavior. [S1]

Sources

References used for this guide.

  1. [S1] Equality comparisons and sameness — MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work