Why declaration of variable in closure environment did not work?
<p>As my understanding, closure makes an environment to link functions and parameters outside the function and allow things inside the function to access them. </p>

<p>As below is a function with closure. When I execute this function three times.</p>

<pre><code>add();
add();
add();
</code></pre>

<p>I expect I should get 1, because everytime I execute add(), It will redeclare counter as 0 and add 1. So it should be <em>counter = 0, counter++, counter = 0, counter++, counter = 0, counter++.</em> But why it turned out to be <strong>3</strong>?</p>

<pre><code>var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

function myFunction(){
    document.getElementById("demo").innerHTML = add();
}
</code></pre>

<p>Second question is :</p>

<pre><code>variable = (function() {})()
</code></pre>

<p>Why we add an extra parenthesis to enclose the function, and why another extra parenthesis after the function?</p>

<p>Thanks!</p>