codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS project: counter

JS project: counter

Build a small app.

Combine variables, events, functions and the DOM. Try adding a reset button or saving the value.

Example code

<style>button{font-size:20px;padding:6px 14px}#n{font-size:48px;margin:10px 0}</style>
<div id='n'>0</div>
<button id='d'>-</button> <button id='r'>Reset</button> <button id='i'>+</button>
<script>
let c=0;
const show=()=>n.textContent=c;
i.onclick=()=>{c++;show();};
d.onclick=()=>{c--;show();};
r.onclick=()=>{c=0;show();};
</script>

Run this code in the online editor

← JS HTML firstLive project: To-do list app →