JavaScript Fundamentals

== vs ===

Understand loose equality, strict equality, type coercion, and predictable JavaScript comparisons.

Learn when JavaScript converts values, why production code usually prefers ===, how null and undefined behave, and why objects are compared by reference.

The core difference

== Loose Equality

Compares values and may perform type coercion before comparing them. For example, "5" == 5 is true because the string can be converted to the number 5.

=== Strict Equality

Compares both value and data type without automatic conversion. "5" === 5 is false because a string and a number are different types.

Analogy: == accepts information that looks similar after conversion. === requires the value and type to match exactly.
String and number
5 == "5"     // true
5 === "5"   // false

Loose equality converts the string; strict equality sees different types.

Boolean and number
true == 1    // true
true === 1  // false
false == 0   // true

Loose equality converts true to 1 and false to 0 before comparing.

null and undefined
null == undefined     // true
null === undefined   // false

This is a special rule of loose equality. Strict equality requires the same type.

Objects
{} == {}       // false
const a = {};
const b = a;
a === b        // true

Objects are compared by reference. Two separate object literals are different objects.

Try the comparison

Edit the two string inputs. The loose comparison uses JavaScript coercion; the strict comparison checks the strings exactly as entered.

"5" == "5"true
"5" === "5"true
Equality Comparison Table
Expression=====Reason
"5" and 5truefalseString converted by ==
true and 1truefalsetrue becomes 1
false and 0truefalsefalse becomes 0
null and undefinedtruefalseSpecial loose-equality rule
{} and {}falsefalseDifferent object references

Best Practices and Interview Answer

Prefer === and !== in almost all situations.
Convert input explicitly with Number(), String(), or Boolean().
Use value == null intentionally when matching null or undefined.
Do not rely on user input coercion in production logic.
Objects compare references, not their visible properties.
Most ESLint configurations recommend strict equality.

30-second answer: == performs loose equality and may convert operand types. === performs strict equality by checking both value and type without conversion, so it is usually more predictable in production code.

Hands-on Exercises
Predict the output
console.log("10" == 10);
console.log("10" === 10);

Work out the result first, then use explicit conversion and strict equality where appropriate.

Explain the difference
console.log(false == "");
console.log(false === "");

Work out the result first, then use explicit conversion and strict equality where appropriate.

Special null case
console.log(null == undefined);
console.log(null === undefined);

Work out the result first, then use explicit conversion and strict equality where appropriate.

Write a safe comparator
function areEqual(value1, value2) {
  return value1 === value2;
}

Work out the result first, then use explicit conversion and strict equality where appropriate.

Practice quiz

== vs === Quiz

Select an answer to see the explanation.

1. What does == perform when operand types differ?

2. What is the result of "5" === 5?

3. Why is true == 1 true?

4. What is the result of null == undefined?

5. Which operator is preferred for predictable production code?

6. What does {} === {} return?

7. Which expression intentionally checks for both null and undefined?

8. What is the safer way to compare a form age value with 18?

Score: 0 / 8