LocalStorage can only store string values. You can use parseInt, which converts the string to an integer:
var new_value = parseInt(localStorage.getItem('num')) + 1
You can also use libraries like store.js to do things automatically for you. All you have to do is enable the library:
<script src="store.js"></script>
Install a new repository:
var numbers = new Store("numbers")
Put things in it:
numbers.set('num', 2)
Get the value and do anything with it:
numbers.get('num') + 1
And you can also go crazy and use several arrays:
numbers.set('nums', [1,2,3])
And change things inside it:
numbers.get('nums')[0] + 3
No type conversion required. You can also store objects, booleans, and more. Just remember to keep things in storage, as it does not do this automatically.
source share