what is the purpose of the wrapping the function declaration of an IIFE in parenthesis?

16 Views Asked by At

How are these two pieces of code different from each other? var a = (function() {//code})(); and var a = function () {//code}(); `

I tried to use both the ways in my code and they both worked well. So, it got me confusing as to why we wrap the function declaration of an IIFE in parenthesis? Here is an example where i use both the ways for the same purpose:

let a = (function () {
  let count = 0;
  return {
    increment : function () {
      console.log(++count);
    }
  }
})();
a.increment();
a.increment();
a.increment();

and

let a = (function () {
  let count = 0;
  return {
    increment : function () {
      console.log(++count);
    }
  }
})();
a.increment();
a.increment();
a.increment();
0

There are 0 best solutions below