JavaScript Academy · Lesson

Loose vs Strict Equality

Understand == versus === comparisons.

Lesson 2 of 413 steps

Loose vs Strict Equality is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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")
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Loose vs Strict Equality” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Loose vs Strict Equality”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

  1. Truthy and Falsy Values
  2. Loose vs Strict Equality
  3. Implicit Coercion in Operations
  4. Explicit Conversion Techniques
← Back to JavaScript Academy