0

While looking at the code, there is (); at the bottom. Can someone tell me what those are called and why is it there?

Below is part of the code. Unfortunately, the rest is too long. I don't want to waste your time on the rest of the code.

  $(window).on('hashchange', function (e) {
      var i = 1;
      setTimeout(function run() {
          //console.log(i + ':hello number');
          if (i < 20) {
              setTimeout(run, 500);
              initstu10msg()
          }
          if (i >= 20) {
              //console.log('msgak');
          }
          i++;
      }, 100)
  }).trigger('hashchange');
  })();
Ayaz Moeen
  • 41
  • 9
  • 1
    This is the end of IIFE. – DecPK Jun 08 '21 at 06:37
  • 1
    The above code example is incomplete because the beginning of the `)()` is not shown. If you were to correctly quote the code you would have seen `(function () {.....})()`. The `()` is just a function call just like `initstu10msg()`. Notice that inside `(...)()` is a function - `function () {...}`. This makes `(...)` return a function. A function can be called so `(...)()` calls the `function () {...}` function – slebetman Jun 08 '21 at 06:44
  • @slebetman, thanks for your comment. The code is a local function; hence there are () at the end of this code. I am trying to understand the simple words; why do we need to add those brackets into the code? – Ayaz Moeen Jun 08 '21 at 07:06
  • 1
    Without the `()`, the function wouldn't run. It would be the same as doing `function start() { console.log("start"); }` and then never doing. `start();` to run it. – Keith Jun 08 '21 at 07:15
  • 1
    Thank you, @Keith, that was a helpful answer. – Ayaz Moeen Jun 08 '21 at 08:31
  • I'm referring to your code. For example what I assume to be the `(function () {...})()` part of your code, meaning I literally expect your code to contain the text `(function ()` and then some other code in the middle and ending in `)()`. And when I mention things like `)()` I literally am referring to the end of `(function () {...})()`. It would be easier to refer to such things in an answer and even easier to do it in some graphical software like Photoshop but I don't think this question merits an answer since there are lots of good answers already existing on stackoverflow – slebetman Jun 08 '21 at 12:49
  • Got my answer... you all are great people... Thank you – Ayaz Moeen Jun 08 '21 at 13:23

0 Answers0