I have found a javascript example and I need an explanation:
<html>
<body>
<button type="button" onclick="myFunction()">Count!</button>
<p id="demo">0</p>
<script>
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
</body>
</html>
I don't understand why the value of var counter get increased every time I press the button while in other place of this tutorial I read: "The lifetime of a JavaScript variable starts when it is declared. Local variables are deleted when the function is completed."