Are All Types of Values in localStorage Stored as a String?

Find out if localStorage stores all types of values as a string

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

Hope you found this post useful. It was published . Please show your love and support by sharing this post.