APIのレスポンスやフォームの値、URLパラメータなどは文字列で渡ってきます。これを true / false として扱いたい場面はよくありますが、ここには初心者がほぼ必ずハマる落とし穴があります。
結論を先に言うと、Boolean("false") は true です。Boolean() や if は文字列の中身を見ず、「空文字かどうか」しか見ません。そのため "false" や "0" も「空でない文字列」としてtrue 扱いになります。正しく変換する方法とあわせて解説します。
"true"/"false" という文字列を真偽値にしたいなら str === "true"(必要なら str.toLowerCase() === "true")を使います。Boolean(str) は使わない(空文字以外すべて true になるため)。【最重要】Boolean(str) と if(str) は中身を見ない
JavaScriptで falsy(偽と評価される値)は false・0・""(空文字)・null・undefined・NaN だけです。つまり文字列で falsy なのは空文字 "" のみで、それ以外の文字列は "false" でも "0" でも空白でもすべて trueです。
console.log(Boolean("false")); // true ← 文字列の "false" も true!
console.log(Boolean("0")); // true ← 文字列の "0" も true(数値の 0 とは別)
console.log(Boolean(" ")); // true ← 空白だけでも true
console.log(Boolean("")); // false ← 空文字だけが false
console.log(Boolean(0)); // false(こちらは数値の 0)
// if も同じ。文字列 "false" でもブロックは実行される
if ("false") {
console.log("実行される!"); // これが表示される
}
const flag = Boolean(apiResponse.enabled) のように書くと、APIが文字列 "false" を返してきても flag は true になります。文字列の中身(”true”/”false”)で判定したいなら、次の方法で明示的に変換してください。“true”/”false” 文字列を boolean に変換する(正しい方法)
文字列 "true" を true に、"false" を false にしたいなら、=== "true" で比較します。これが最もシンプルで確実です。
const str = "false";
const bool = str === "true";
console.log(bool); // false("true" と一致しないので false)
console.log("true" === "true"); // true
console.log("false" === "true"); // false
// 大文字小文字を無視したいとき
const bool2 = "TRUE".toLowerCase() === "true";
console.log(bool2); // true
“1” / “yes” / “on” など様々な表現に対応する
チェックボックスや設定値は "1" / "yes" / "on" など様々な形で「真」を表すことがあります。これらをまとめて扱うには、真とみなす値のリストで判定します。
function toBool(value) {
return ["true", "1", "yes", "on"].includes(
String(value).trim().toLowerCase()
);
}
console.log(toBool("true")); // true
console.log(toBool("false")); // false
console.log(toBool("1")); // true
console.log(toBool("0")); // false
console.log(toBool("YES")); // true(大文字でもOK)
console.log(toBool("")); // false
console.log(toBool(" on ")); // true(前後空白も許容)
JSON.parse を使う方法と注意
"true" / "false" はJSONとしても有効なので JSON.parse() でも変換できます。ただし "yes" のようなJSONでない文字列を渡すと例外になるため、エラー処理が必要です。
console.log(JSON.parse("true")); // true(boolean)
console.log(JSON.parse("false")); // false(boolean)
console.log(JSON.parse("1")); // 1(数値。boolean ではない点に注意)
// JSON として無効な文字列は例外になる
try {
JSON.parse("yes"); // SyntaxError
} catch (e) {
console.log("変換できません:", e.name); // "SyntaxError"
}
実用例:フォーム・API の値を真偽値にする
// チェックボックス/data属性などの文字列を真偽値に
const isAgreed = toBool(document.getElementById("agree").value); // "true"/"false" 等
// APIレスポンス(文字列で来ることがある)
const data = { enabled: "false" };
const enabled = data.enabled === "true";
console.log(enabled); // false(Boolean(data.enabled) だと誤って true になる)
// URLパラメータ
const params = new URLSearchParams(location.search);
const debug = toBool(params.get("debug")); // ?debug=1 や ?debug=true で true
方法の使い分け
| 方法 | “false”の結果 | 用途 |
|---|---|---|
Boolean(str) / !!str |
true(罠) | 「空文字でないか」だけ見たいとき |
str === "true" |
false | “true”/”false” の変換(推奨) |
toBool(str) |
false | “1”/”yes”/”on” など多様な表現に対応 |
JSON.parse(str) |
false | JSONとして厳密に(例外処理が必要) |
"true"/"false" という文字列を真偽値にしたい」なら === "true"、「"1"/"yes" なども許容したい」なら toBool()。Boolean(str) は文字列の中身判定には使わないのが鉄則です。よくある質問(FAQ)
Boolean() は文字列の中身ではなく「空文字かどうか」だけを見るためです。"false" は空でない文字列なので true になります。falsy な文字列は空文字 "" だけで、それ以外("0"・"false"・空白)はすべて true です。str === "true" が最もシンプルで確実です。大文字小文字を無視するなら str.toLowerCase() === "true"、"1"/"yes" なども許容するなら記事内の toBool() 関数を使います。0 は falsy(false 扱い)ですが、文字列の "0" は truthy(true 扱い)です。Boolean(0) は false、Boolean("0") は true になります。文字列を数値として判定したい場合は文字列を数値に変換できるか判定する方法を参照してください。まとめ
JavaScriptで文字列を真偽値として扱う方法のポイントを整理します。
- 文字列で falsy なのは空文字
""のみ。"false"・"0"・空白はすべて true Boolean(str)/if(str)は中身を見ない → 文字列の真偽判定には使わない- “true”/”false” の変換は
str === "true"(大小無視はtoLowerCase()) - “1”/”yes”/”on” など多様な表現は
toBool()関数で対応 JSON.parseも使えるが、JSON以外の文字列は例外になるので注意
関連として、真偽値を反転する方法・文字列を数値に変換できるか判定する方法・文字列を数値型に変換する方法もあわせて確認すると、型変換まわりに強くなれます。
