| name | boolean-string-trap |
| description | JavaScript boolean-string coercion trap — "false" is truthy, JSON.parse or strict comparison required |
| lastReviewed | 2026-04-30T00:00:00.000Z |
Boolean String Trap
Category: JavaScript
Time Saved: 30+ minutes debugging "why is false truthy?"
Battle-tested: Yes — Alex extension, multiple web apps
The Problem
Your feature toggle isn't working. if (showFeature) always evaluates to true, even when the user disabled it. You check localStorage and it clearly shows "false".
Why It Happens
localStorage, sessionStorage, URL params, and form inputs all return strings. The string "false" is truthy in JavaScript because it's a non-empty string.
"false"
"0"
"null"
"undefined"
false
0
null
undefined
""
The Rule
Use strict comparison or explicit parsing — never rely on truthiness for string-sourced values
const raw = localStorage.getItem('darkMode');
if (raw) { enableDarkMode(); }
const raw = localStorage.getItem('darkMode');
if (raw === 'true') { enableDarkMode(); }
const darkMode = JSON.parse(localStorage.getItem('darkMode') || 'false');
if (darkMode) { enableDarkMode(); }
Common Sources of String Booleans
| Source | Returns | Solution |
|---|
localStorage.getItem() | string | null | === 'true' or JSON.parse() |
sessionStorage.getItem() | string | null | === 'true' or JSON.parse() |
URLSearchParams.get() | string | null | === 'true' |
FormData.get() | string | null | === 'true' |
process.env.VAR | string | undefined | === 'true' |
dataset.myAttr | string | === 'true' |
TypeScript Pattern
function parseBooleanParam(value: string | null): boolean {
if (value === null) return false;
return value.toLowerCase() === 'true';
}
const showAdvanced = parseBooleanParam(localStorage.getItem('showAdvanced'));
VS Code Settings Pattern
vscode.workspace.getConfiguration().get() returns the actual type (boolean, number, etc.) — no parsing needed. But if you cache it to localStorage, you're back to strings.
const enabled = config.get<boolean>('myExtension.enabled');
localStorage.setItem('cached', String(enabled));
const cached = localStorage.getItem('cached') === 'true';
Verification
console.log(typeof value, value, Boolean(value));
Source: Promoted from AI-Memory global-knowledge.md (2026-04-27)