Search code examples
javascriptiife

Why iife not working in a simple example?


I can't understand why functional expression call doesn't work and throws an error.

Can you explain it to me?

var a = function (x) {
  alert(x)
}

(function() {
   a(1);
}());

Thanks to everyone

The task was much easier than I thought


Solution

  • That is because JS is parsing the IIFE as an argument call for the function, do it like this with an added semi-colon

    var a = function (x) {
      alert(x)
    };    
    (function() {
       a(1);
    }());