Get your own website Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Closures</h1>
<p>Counting with a function variable.</p>
<p id="demo"></p>
<script>
// Function to increment counter
function add() {
  let counter = 0;
  counter += 1;
  return counter;
}
let x = 0;
// Call add() 3 times
x = add();
x = add();
x = add();
// The counter should now be 3. But it is 1.
document.getElementById("demo").innerHTML = "The counter is: " + x;
</script>
</body>
</html>