الانتقال إلى المحتوى
FrontendJavaScript· 7 دقائق للقراءة

== vs === Isn't 'Value vs Type': How JavaScript Equality Really Works

When the types match, == is ===. When they don't, == runs a fixed list of conversions first.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • ES2025
محتويات المقال

A settings page has a "Max downloads" field where 0 means unlimited. QA reports that clearing the field makes the summary say "Unlimited". The code reads if (limit == 0), and limit comes straight from the input's value. A teammate changes it to === because the linter complains, and now typing 0 doesn't show "Unlimited" either. Neither operator was the bug. The code compared a string with a number and never decided what an empty field means.

The common misconception

The belief: === compares value and type, == compares the value and ignores the type, and that's all there is to equality. Use === everywhere and the problem goes away.

It's a good first rule, and MDN agrees with its conclusion: strict equality is almost always the comparison you want. The weak part is "ignores the type". == doesn't ignore types; it converts one operand to match the other in specific steps, and those steps are what make "" == 0 true and "true" == true false. And === doesn't compare "values" the way people picture it: two objects with the same contents are not equal, and NaN isn't even equal to itself.

What actually happens

When the types match, == is ===

The spec defines == through an abstract operation called IsLooselyEqual. Its very first step says: if both operands have the same type, return IsStrictlyEqual, the operation behind ===. So x == y and x === y can only disagree when x and y have different types. Comparing two strings, two numbers or two objects with == is exactly a === comparison, with no conversion at all.

When they don't, == follows a short list of conversions

For mixed types, IsLooselyEqual matches the pair against a few cases, converts one side, and compares again:

  1. null and undefined are equal to each other and to nothing else, not even 0 or false.
  2. A number against a string: the string becomes a number. An empty or whitespace-only string becomes 0, and something like "12px" becomes .
  3. A boolean on either side becomes a number first: true is 1, false is 0.
  4. An object against a primitive: the object is converted to a primitive. An array becomes its comma-joined string, so [] becomes "".
  5. A BigInt is compared with a number by mathematical value, and with a string after parsing the string as a BigInt.
  6. Any other mix, such as a symbol against a string, is not equal.

Rule 3 surprises people most. "true" == true doesn't ask whether the string looks truthy: true becomes 1, then "true" becomes NaN, and NaN equals nothing. This automatic conversion is called , and it explains every "weird" == result people love to share.

=== has its own rules for numbers and objects

IsStrictlyEqual returns false for different types. For two numbers it uses numeric equality, where NaN is not equal to any value, itself included, and +0 equals -0. For everything else it checks whether both sides are the same value, and for objects that means the same object in memory, known as . Two { id: 1 } literals are two different objects, with == or ===:

JavaScript equality.js
console.log("" == 0); // true: "" converts to the number 0console.log("true" == true); // false: true → 1, "true" → NaNconsole.log([] == false); // true: [] → "" → 0, false → 0console.log(null == 0); // false: null only equals undefinedconsole.log(NaN === NaN); // false: NaN equals nothingconsole.log({ id: 1 } === { id: 1 }); // false: two different objectsconsole.log([NaN].indexOf(NaN)); // -1: indexOf uses ===console.log([NaN].includes(NaN)); // true: includes uses SameValueZero

Four comparisons, not two

The last two lines show that the language has more than two kinds of equality. MDN lists four algorithms, and built-in methods pick different ones. That is why indexOf can't find NaN but includes can:

AlgorithmUsed byNaN vs NaN+0 vs -0Converts types?
IsLooselyEqual==, !=not equalequalyes
IsStrictlyEqual===, !==, indexOf, switch casesnot equalequalno
SameValueZeroincludes, Map and Set keysequalequalno
SameValueObject.isequalnot equalno

Fixing the settings bug

Back to the download limit. An input's value is always a string, so limit == 0 matches "0", "" and " ". Switching to === makes it match nothing, because a string is never === a number. The fix isn't a different operator. It's converting once at the edge and deciding what an empty field means:

JavaScript limit.js
// limit comes from an <input>'s value, so it is always a stringfunction describeLimit(limit) {  if (limit == 0) return "Unlimited"; // "0", "" and "  " all match  return `${limit} downloads`;}console.log(describeLimit("0")); // Unlimitedconsole.log(describeLimit("")); // Unlimited, but the field is emptyconsole.log(describeLimit("5")); // 5 downloads

The === in the second version isn't doing anything clever. It works because both sides are already numbers or null, so there is no conversion left to surprise anyone.

How to apply it in your project

  1. Convert at the boundaryTurn form values, query parameters and storage values into the type you mean, with Number() or an explicit check for "", right where they enter your code.
  2. Default to ===Once types are settled, === says exactly what you mean. Keep == null only where it clearly means "missing".
  3. Use the right tool for NaN and zerosUse Number.isNaN(x) to detect NaN, includes instead of indexOf when an array may hold NaN, and Object.is when the sign of zero matters.
  4. Compare objects by their fieldsNeither operator looks inside objects. Compare the field that defines identity, like an id, or write a small function that compares the fields you care about.

Key takeaways

  • == and === behave identically when both operands have the same type; they only differ on mixed types.
  • == converts in specific steps: null and undefined only match each other, booleans and strings become numbers, objects become primitives.
  • === isn't just "value and type": NaN never equals itself, +0 equals -0, and objects compare by identity.
  • Object.is, includes, Map and Set use their own sameness rules, so NaN and -0 behave differently there.
  • Fix comparisons by converting inputs once at the edge; swapping the operator alone just moves the bug.

📚 المصادر

  1. Equality comparisons and sameness — MDN
  2. Equality (==) — MDN
  3. Strict equality (===) — MDN
  4. Object.is() — MDN
  5. IsLooselyEqual (x, y) — ECMAScript Language Specification

التعليقات (0)