Skip to main content

Booleans and Null

Learning Focus

true, false, and null look trivial but cause frequent bugs when languages apply different coercion semantics.

Boolean

Written in lowercase only — capitals cause a parse error.

{
"isActive": true,
"isDeleted": false,
"hasAccess": true
}

Language Mapping

JSONPythonJavaScriptGoPHP
trueTruetruetruetrue
falseFalsefalsefalsefalse

Null

Represents the intentional absence of a value. Also lowercase only.

{ "middleName": null, "deletedAt": null }

null vs Missing Key

{ "a": null } // key exists, value is null
{ } // key does not exist at all

These are semantically different. Handle both cases explicitly:

data = {"a": None}
print("a" in data) # True — key present, value null
print("b" in data) # False — key truly missing

Type Coercion Traps (JavaScript)

// ⚠️ Dangerous: loose equality
if (!apiResponse.value) {
// Triggers for null, false, 0, "" — not just null!
}

// ✅ Safe: explicit check
if (apiResponse.value === null) { /* ... */ }
if (apiResponse.value === false) { /* ... */ }

Concept Map

Concept Flow

JSON Literals
├── true / false — lowercase only (parse error otherwise)
└── null — lowercase only
└── null ≠ missing key ≠ empty string ≠ 0

Common Pitfalls

PitfallConsequencePrevention
True / False in raw JSONParse errorUse lowercase in JSON strings
Treating null as missing keyLogic errorsCheck key existence separately
Coercing 0 / "" to booleanSilent logic bugsUse strict equality
Python None via f-stringProduces "None" string, not nullAlways use json.dumps()

What's Next