Why the output is 3? how closure is being used here?

var add = (function () {
var counter = 0;
return function () {counter += 1; return counter}
})();

add();
add();
add();

and significance of “()” after ‘add’ function definition
this is the same example as you have given in the video but implementation is confusing

Here we create a function and before storing it in add variable, we call it using (). The result of that function call, which is also a function, is stored in variable add. Thus further add() will run the previously returned function, which increases the counter.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.