Both, keys and values stored in window.localStorage
, are stored as strings (more precisely, in UTF-16 DOMString format). All non-string values are converted into a string (as you can see in the following examples):
localStorage.setItem('foo', 123);
const value = localStorage.getItem('foo');
console.log(value); // '123'
console.log(typeof value); // 'string'
console.log(value === '123'); // true
localStorage.setItem('foo', true);
const value = localStorage.getItem('foo');
console.log(value); // 'true'
console.log(typeof value); // 'string'
console.log(value === 'true'); // true
localStorage.setItem('foo', null);
const value = localStorage.getItem('foo');
console.log(value); // 'null'
console.log(typeof value); // 'string'
console.log(value === 'null'); // true
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.