# JavaScriptで文字列falseをBoolean型に変更しようとして沼に落ちた

Table of Contents

文字列をBoolean型に変換するとtrueになる

console.log(Boolean('false'))
// true

特に何も考えず上記コードを書きデバック時にはまった。

他の型は上記のような書き方でうまく変換できるので、原因解明に時間がかかった。

Boolean()を調べてみると、

注意: 文字列への型強制や数値への型強制などの他の型の変換とは異なり、論理値への型強制はオブジェクトからプリミティブに変換しようとするわけではありません。

どうやらこのようなことらしい。

じゃあ、いったいどうすればいいのか。

文字列falseをBoolean型にする方法

console.log(JSON.parse(('false').toLowerCase()))

上記コードでうまく変換できた。

My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts

Comments