0

I want to add +1 on every page reload at localStorage, but it doesn't work:

var Counter = localStorage.getItem('Counter');

localStorage.setItem('Counter', Counter + 1);

var CounterStorage = console.log(localStorage.getItem('Counter'));

My results on page reload:

  1. 1
  2. 11
  3. 111

But what I need is:

  1. 1
  2. 2
  3. 3

Where did I make a mistake?

Pepe
  • 960
  • 9
  • 21
  • 1
    `localStorage` only holds strings. To perform mathematical addition you need to convert the value to an int or float - `var Counter = parseInt(localStorage.getItem('Counter') || 0, 10);` – Rory McCrossan Oct 05 '21 at 12:54
  • 1
    Ah, ok, good to know - thx! – Pepe Oct 05 '21 at 12:54

0 Answers0