Loose vs Strict Equality
Understand == versus === comparisons.
Loose vs Strict Equality is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Equality Operators
JavaScript has two equality operators. Strict equality (triple equals) compares without conversion. Loose equality (double equals) converts types before comparing.
console.log(1 === 1)
console.log(1 == "1")Strict Equality
Strict equality returns true only when both the type and the value match. No coercion happens.
console.log(5 === 5)
console.log(5 === "5")
console.log(true === 1)Loose Equality Coerces
Loose equality attempts to convert operands to a common type before comparing, which can produce surprising true results.
console.log(5 == "5")
console.log(true == 1)
console.log(0 == false)Number and String
When comparing a number and a string with loose equality, the string is converted to a number first.
console.log("10" == 10)
console.log("10" === 10)Boolean Coercion
With loose equality a boolean is converted to a number, so true becomes 1 and false becomes 0 before comparison.
console.log(true == 1)
console.log(false == 0)
console.log(true == 2)Null and Undefined
Loose equality treats null and undefined as equal to each other, but not to anything else like zero.
console.log(null == undefined)
console.log(null === undefined)
console.log(null == 0)The NaN Exception
NaN is not equal to anything, including itself, under either operator. Use Number.isNaN to test for it.
console.log(NaN === NaN)
console.log(NaN == NaN)
console.log(Number.isNaN(NaN))Object References
Objects compare by reference, not by contents. Two distinct objects with identical properties are never equal.
const a = { x: 1 }
const b = { x: 1 }
console.log(a === b)
console.log(a === a)Surprising Loose Results
Loose equality can yield counterintuitive results. An empty string equals zero, and an empty array equals false after coercion.
console.log("" == 0)
console.log([] == false)
console.log("0" == false)Prefer Strict Equality
Most style guides recommend using strict equality by default to avoid hidden coercion bugs. Reach for loose equality only with a deliberate reason.
const value = "42"
if (value === "42") {
console.log("matched as string")
}Checking for Null or Undefined
One accepted use of loose equality is checking for null or undefined together, since the double-equals null pattern covers both.
function check(v) {
if (v == null) {
return "missing"
}
return "present"
}
console.log(check(undefined))
console.log(check(0))Quick Check
Test your equality knowledge.
Recap
Recap: Triple equals avoids coercion; double equals applies it.
- Strict checks type and value
- Loose converts to a common type first
- null equals undefined loosely only
- Prefer strict equality by default
console.log(1 === 1)
console.log(1 == "1")Frequently asked questions
Is the “Loose vs Strict Equality” lesson free?
Yes — the full text of “Loose vs Strict Equality” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Loose vs Strict Equality”?
Understand == versus === comparisons. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Loose vs Strict Equality” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Truthy and Falsy Values
- Loose vs Strict Equality
- Implicit Coercion in Operations
- Explicit Conversion Techniques