Table of Contents
In this project, you will build a counter with buttons to:
onclick attributes)localStorage (to Save and Restore the counter)Create the basic HTML layout containing the counter display and buttons:
<h2> header element.<p> element displaying a number.<button> elements.onclick attribute to each button to trigger JavaScript functions.<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>

Result View Example
Create a script that:
let count = 0updateCount() to update the displaydocument.getElementById().innerHTML to display the counter value<script>
// Declare a counter
let count = 0;
// Function to display the counter
function updateCount() {
document.getElementById("count").innerHTML = count;
}
</script>

Result View Example
increaseCount() to handle incrementing.count++ to increase the counter.updateCount() to refresh the display.// Function to increase the counter
function increaseCount() {
count++;
updateCount();
}

Result View Example
decreaseCount() to handle decrementing.count-- to decrease the counter.updateCount() to refresh the display.// Function to decrease the counter
function decreaseCount() {
count--;
updateCount();
}

Result View Example
resetCount() to reset.count = 0 to reset the counter value.updateCount() to refresh the display.// Function to reset the counter
function resetCount() {
count = 0;
updateCount();
}

Result View Example
saveCount() to save.localStorage.setItem() to store the current count value as text.// Function to save the counter
function saveCount() {
localStorage.setItem("count", count);
}

Result View Example
loadCount() to load.localStorage.getItem() to get the saved value.Number(saved) to convert the saved text back into a number.updateCount() to refresh the display.// Function to load the counter
function loadCount() {
let saved = localStorage.getItem("count");
if (saved !== null) {
count = Number(saved);
}
updateCount();
}
[!NOTE]
localStoragestores values as text, so we useNumber()to convert the value back into a number.

Result View Example
Make the counter start at 10 instead of 0.
Prevent the counter from going below 0.
Automatically load the saved counter value when the page opens.
<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>

Result View Example
Document in project
You can Download PDF file.