How to store localStorage data after page is refreshed?

  javascript, local-storage

2 things I would like to accomplish here:

  1. I’m trying to display the new high score after the page is refreshed.
  2. The high score resets to 0 after refreshing due to localStorage.setItem('high_score', 0); so I’m not sure how to properly set this without resetting it to 0 every time the page is refreshed.
        let score = 0;
        var high_score = 0;
        localStorage.setItem('high_score', 0);

        document.getElementById('high_score').innerHTML="HIGH<br>SCORE:<br>" + 
        localStorage['high_score'];

        if (score > parseInt(localStorage.getItem('high_score'), 10)) {
            localStorage.setItem('high_score', score);
        }

Source: Ask Javascript Questions

LEAVE A COMMENT