JavaScript Interview Question Equality Loose vs Strict
How to answer the double versus triple equals interview question: loose equality coerces, strict does not. See the rules, NaN, Object.is and null checks.

The question "what is the difference between == and ===" is easy to answer badly. Saying that === compares types as well as values is true as far as it goes, and it is the answer that tells the interviewer you stopped reading after the first paragraph of a tutorial. They are waiting for the next three sentences: what == actually does to its operands, which of its results are surprising, and when you would still use it. This post gives the answer in thirty seconds, the coercion rules in the order the language applies them, the table of results that interviewers like to ask about, NaN and Object.is, the one place where loose equality is the idiomatic choice, and the wrong answers to avoid. The examples run on Node.js 24, and every output is exact.
The answer in thirty seconds
=== is strict equality: if the two values have different types, the result is false, and otherwise it compares them directly. == is loose equality: if the types differ, it first converts one or both operands with a fixed set of rules and then compares. Strict equality has no coercion step, so its results are predictable, and most style guides require it. The single common exception is value == null, which is true for both null and undefined and nothing else.
console.log(1 === 1, 1 === "1", 1 == "1");
console.log(null === undefined, null == undefined);
console.log({} === {}, [] === [], "a" === "a");
console.log(NaN === NaN, NaN == NaN);true false true
false true
false false true
false falseThe number 1 and the string "1" differ in type, so === says no and == converts the string to a number and says yes. Two empty objects are not equal under either operator, because objects are compared by identity: two separate objects are two different things. NaN is not equal to anything, including itself, which the last line shows.
How loose equality decides
The algorithm has a short list of rules, and you do not need all of them to answer well. These are the ones that matter.
If both values have the same type, compare as
===does.nullandundefinedare equal to each other and to nothing else.If one operand is a number and the other a string, convert the string to a number.
If one operand is a boolean, convert it to a number first, then compare again.
If one operand is an object and the other a string, number or symbol, convert the object to a primitive, then compare again.
console.log("5" == 5, "5" == "05", 5 == "05");
console.log(true == 1, true == "1", true == "true");
console.log(null == 0, null == "", undefined == 0);
console.log(0 == "", 0 == "0", "" == "0");true false true
true true false
false false false
true true falseWalk the first line with the rules. "5" == 5 converts the string, giving 5 == 5. "5" == "05" has two strings, the same type, so it compares text and says no. 5 == "05" converts the string "05" to 5 and says yes. On the second line, true becomes 1 first, which is why true == "1" is true: both sides turn into the number 1. The string "true" becomes NaN, so true == "true" is false. Notice the third line: null is not equal to 0 or to the empty string, because rule 2 says it equals only undefined.
The results interviewers ask about
console.log([] == false, [] == ![], [0] == false, [1] == 1);
console.log("" == 0, "0" == false, " \t\n" == 0);
console.log(NaN != NaN, [NaN].includes(NaN), [NaN].indexOf(NaN));
console.log(new String("a") == "a", new String("a") === "a");true true true true
true true true
true true -1
true falseThe first line is the famous one. [] == false converts the boolean to 0, then the array to a primitive, which is the empty string, then the empty string to 0, and 0 == 0 is true. The expression [] == ![] is [] == false, since a non-empty value negated is false, so it is true as well. A string made only of whitespace converts to 0. On the third line, indexOf() uses strict equality and cannot find NaN, while includes() uses a slightly different comparison, covered below, and can. The last line shows an object wrapper compared to a string: loose equality converts the wrapper, and strict equality sees two different types.
Strict equality and NaN
=== has two special cases that come from the floating point standard.
console.log(0 === -0, Object.is(0, -0));
console.log(NaN === NaN, Object.is(NaN, NaN));
console.log(Number.isNaN(NaN), Number.isNaN("abc"), isNaN("abc"));
console.log(Object.is(1, 1), Object.is("a", "a"), Object.is({}, {}));true false
false true
true false true
true true falsePositive and negative zero are equal under ===, and Object.is() distinguishes them. NaN is not equal to itself under ===, and Object.is() says it is. Object.is() is the tool for the two edge cases and otherwise matches ===. The third line separates the two functions that test for NaN: the global isNaN() converts its argument to a number first, so "abc" becomes NaN and it returns true, while Number.isNaN() returns true only for the actual value NaN. Prefer Number.isNaN().
Three more equality algorithms
The language has more than one definition of "same", and the interviewer may ask where each is used.
const items = [NaN, 0, -0];
console.log(items.includes(NaN), items.indexOf(NaN));
console.log(new Set([NaN, NaN, 0, -0]).size);
console.log(new Map([[NaN, "found"]]).get(NaN));
switch ("1") {
case 1:
console.log("number case");
break;
case "1":
console.log("string case");
break;
}true -1
2
found
string caseincludes(), Set and Map use SameValueZero: like ===, except that NaN equals NaN, and 0 and -0 are treated as the same. That is why the set holds two items, NaN and 0. A switch statement uses strict equality, so the string "1" does not match the number case. In summary, === for most code, Object.is() when you must tell 0 from -0, and SameValueZero inside collections.
Comparing objects
Neither operator compares contents.
import { isDeepStrictEqual } from "node:util";
const a = { id: 1, tags: ["x"] };
const b = { id: 1, tags: ["x"] };
console.log(a === b, a == b);
console.log(a === a);
console.log(JSON.stringify(a) === JSON.stringify(b));
console.log(isDeepStrictEqual(a, b));
const reordered = { tags: ["x"], id: 1 };
console.log(JSON.stringify(a) === JSON.stringify(reordered));
console.log(isDeepStrictEqual(a, reordered));false false
true
true
true
false
trueTwo objects with the same contents are different objects. Comparing JSON.stringify output works for simple data, and breaks when key order differs, as the reordered object shows, and for values that JSON drops, such as undefined. A deep comparison function, such as isDeepStrictEqual() from node:util or a library equivalent, compares the structure. In an interview, say that you would write or import one, and name the limits of the JSON trick.
The place where loose equality is idiomatic
function describe(value) {
return value == null ? "nothing" : `got ${value}`;
}
console.log(describe(null), describe(undefined), describe(0), describe(""), describe(false));
const strict = (value) => value === null || value === undefined;
console.log(strict(null), strict(undefined), strict(0));nothing nothing got 0 got got false
true true falsevalue == null is the one comparison where loose equality is shorter and exactly right: it is true for null and undefined, and false for every other value, including 0, the empty string and false. Many style guides that ban == make this exception, and linters have a setting for it. Say this in the interview: it shows you know the rules well enough to know where they are safe. The ?? operator and optional chaining use the same idea, treating only those two values as missing.
Relational operators coerce too
console.log("10" < "9", 10 < "9", "10" < 9);
console.log(null >= 0, null == 0, null > 0);
console.log(undefined >= 0, undefined == 0);
console.log("a" < "b", "B" < "a", "apple" < "apricot");true false false
true false false
false false
true true trueWhen both operands are strings, < compares text, so "10" comes before "9". If one is a number, the other is converted. The null line is a classic quirk: relational operators convert null to 0, so null >= 0 is true, while == treats null specially and says null == 0 is false. The two operators disagree because they follow different rules. Do not rely on either for null. When you want to sort or compare text from a user, convert explicitly, with Number() for numbers, and localeCompare() or Intl.Collator for text.
Wrong answers that lose the point
"
===is faster, so always use it." The difference is not a reason, and engines optimize both. Use it because the results are predictable."
==compares values and===compares types." Strict equality compares values after checking the type; loose equality converts first."
==is evil and never used." The== nullcheck is idiomatic."
NaN === NaNis true." It is false, andObject.is(NaN, NaN)is true.
A mistake we hit: a form value compared to a number
const form = { quantity: "3" };
const limit = 3;
console.log(form.quantity == limit);
console.log(form.quantity === limit);
console.log(Number(form.quantity) === limit);
const cases = ["3", " 3 ", "3.0", "0x3", "three", ""];
console.log(cases.map((c) => c == 3));
console.log(cases.map((c) => Number.isInteger(Number(c)) && c.trim() !== ""));true
false
true
[ true, true, true, true, false, false ]
[ true, true, true, true, false, false ]A loose comparison made the form value work, and made a validator accept strings such as "0x3" and " 3 " that nobody intended. The strict version failed on the plain case and exposed that the value was a string. The fix we use is to convert once, on the way in, with Number(), validate the result, and compare strictly afterwards. The two arrays agree here, but Number("") is 0 and not NaN, so the empty string needs its own check, which is why the second line includes c.trim() !== "".
Choosing between the operators
| You want | Use |
|---|---|
| A normal comparison | === |
To test for null or undefined together | value == null |
To tell 0 from -0, or to match NaN | Object.is() |
To check for NaN | Number.isNaN() |
To find NaN in an array | includes() |
| To compare object contents | A deep-equal function |
Practical rules we follow
Use === and !== by default. Allow == null and nothing else. Convert input once at the boundary with Number() or String(), and compare strictly after that. Use Number.isNaN(). Do not compare objects with either operator. Turn on a linter rule such as eqeqeq with the null exception. And in an interview, give the algorithm in two or three sentences and then show a table of results.
Where the equality answer stops being enough
A deeper question asks for the full algorithm, including BigInt and symbols, which are not coerced by == in the usual ways: 1n == 1 is true and 1n === 1 is false. It may also ask about ToPrimitive and valueOf, which let objects control their coercion. The reference is in the MDN equality comparisons page and the ECMAScript specification.


