7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

localstorageに直接true/falseを格納しようとした時にハマったこと

Last updated at Posted at 2019-10-01

ハマったこと

localstorage上にtrue/falseを格納して、取り出してifしようとしたところ、falseを取り出してるのにifをtrueで抜けてきた

localstorage.setItem('setVariable',true)

参考記事

JavaScriptで"false"が文字列扱いされる場合の覚書

解決方法

原因としては、true/falseを格納してもStringで格納されてしまうので、1/0で格納する。

localstorage.setItem('setVariable',1)

取り出して来た時にparseIntで数値化して、Booleanで真偽値型に変更する

Boolean(parseInt(localstorage.getItem('setVariable'),10))
7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?