javascript - 是否可以像在 Javascript 对象中那样在 localStorage 中存储整数值并在不进行类型转换的情况下提取它?

标签 javascript html local-storage

当我将整数值分配给 localStorage 项时

localStorage.setItem('a',1)

并检查它的类型

typeof(localStorage.a)
"string"

它返回string,我可以将它转换为int以供我使用

parseInt(localStorage.a)

我的问题是,是否可以在 localStorage 中存储整数值,就像我可以在不进行类型转换的情况下对 Javascript 对象所做的那样?

a={};
a.number=1;
typeof(a.number)
"number"

最佳答案

My question is it possible to store integer value inside localStorage as I can do for Javascript objects without typecasting?

没有。

Storage objects are simple key-value stores, similar to objects, but they stay intact through page loads. The keys can be strings or integers, but the values are always strings. [source]

关于javascript - 是否可以像在 Javascript 对象中那样在 localStorage 中存储整数值并在不进行类型转换的情况下提取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952287/

相关文章:

python - 如何使用python请求修改本地存储

javascript - 使用javascript在iframe中打开本地pdf文件

javascript - JQuery 选择器性能和比较问题

javascript - 一次只有一个 Active 类和 Visible Div

javascript - 用javascript数组填充html

html - 动画子菜单纯css

javascript - "Input type file.files"选择器在 jQuery 中不起作用

javascript - 这个 Angular 代码有什么问题?

javascript - 在 React Router 上,如何保持登录状态甚至页面刷新?

cordova - 电话间隙 : can't make localStorage persist