I saw closure being declared using function expression and not function declaration. Being new to java script i have below queries. i tried referring similar question but it didn't helped me.
- Do closure always need to be declared using function expression and not function definition
- Do closure need to be self invoked in function expression
- I understand key difference between Function declaration and function expression id hoisting. can anyone help me when and which in situation i should decide whether i should go with function declaration or function expression. what are the other advantages and disadvantages of using any one of them
Sorry some questions might be answered previously or kind of basic but being new to it i feel its confusing
below is the example i have done using function declaration but its not giving me proper result
function counter() {
var count = 0;
return function() { return count++; }
}
<!-- using function declaration -->
<button onclick="alert(counter())">Counter</button>
var counter = (function() {
var count = 0;
return function() { return count++; }
increment();
})();
<button onclick="alert(counter())">Counter</button>