JavaScript localStorage Counter

Back to JS page

Table of Contents

Project Overview

In this project, you will build a counter with buttons to:

Project Skills Required:

1. Create the HTML

Create the basic HTML layout containing the counter display and buttons:

<h2>Counter</h2>

<p id="count" style="font-size:40px;">0</p>

<button onclick="increaseCount()">+</button>
<button onclick="decreaseCount()">-</button>
<button onclick="resetCount()">Reset</button>
<button onclick="saveCount()">Save</button>
<button onclick="loadCount()">Load</button>

Example 1

Result View Example

2. Create a Script

Create a script that:

<script>
// Declare a counter
let count = 0;

// Function to display the counter
function updateCount() {
  document.getElementById("count").innerHTML = count;
}
</script>

Example 2

Result View Example

3. Add an Increase Counter Function

// Function to increase the counter
function increaseCount() {
  count++;
  updateCount();
}

Example 3

Result View Example

4. Add a Decrease Counter Function

// Function to decrease the counter
function decreaseCount() {
  count--;
  updateCount();
}

Example 4

Result View Example

5. Add a Reset Counter Function

// Function to reset the counter
function resetCount() {
  count = 0;
  updateCount();
}

Example 5

Result View Example

6. Create a Save Counter Function

// Function to save the counter
function saveCount() {
  localStorage.setItem("count", count);
}

Example 6

Result View Example

7. Create a Load Counter Function

// Function to load the counter
function loadCount() {
  let saved = localStorage.getItem("count");
  if (saved !== null) {
    count = Number(saved);
  }
  updateCount();
}

[!NOTE]
localStorage stores values as text, so we use Number() to convert the value back into a number.

Example 7

Result View Example

Exercises & Solutions

Exercise 1

Make the counter start at 10 instead of 0.

Exercise 2

Prevent the counter from going below 0.

Exercise 3

Automatically load the saved counter value when the page opens.

Solutions Code

<h2>Counter</h2>

<p id="count" style="font-size:40px;">0</p>

<button onclick="increaseCount()">+</button>
<button onclick="decreaseCount()">-</button>
<button onclick="resetCount()">Reset</button>
<button onclick="saveCount()">Save</button>
<button onclick="loadCount()">Load</button>

<script>
// Declare the counter (starts at 10)
let count = 10;

// Load the counter when the page opens
loadCount();

// Function to display the counter
function updateCount() {
  document.getElementById("count").innerHTML = count;
}

// Function to increase the counter
function increaseCount() {
  count++;
  updateCount();
}

// Function to decrease the counter (prevent going below 0)
function decreaseCount() {
  if (count > 0) {
    count--;
    updateCount();
  }
}

// Function to reset the counter (resets to 10)
function resetCount() {
  count = 10;
  updateCount();
}

// Function to save the counter
function saveCount() {
  localStorage.setItem("count", count);
}

// Function to load the counter
function loadCount() {
  let saved = localStorage.getItem("count");
  if (saved !== null) {
    count = Number(saved);
  }
  updateCount();
}
</script>

Example 8

Result View Example

Document

Document in project

You can Download PDF file.

Reference