0

I get confused by this function:

! function(e, t, n, c, r, a, i) {
    e.Newsletter2GoTrackingObject = r, e[r] = e[r] || function() {
        (e[r].q = e[r].q || [])
        .push(arguments);
    }, e[r].l = 1 * new Date, a = t.createElement(n), i = t.getElementsByTagName(n)[0], a.async = 1, a.src = c, i.parentNode.insertBefore(a, i);
}(window, document, "script", "https://static.newsletter2go.com/utils.js", "n2g");

What is the meaning of the ! before the function definition and why are there parameters outside of the function declaration? I mean this: (window, document, "script", "https://static.newsletter2go.com/utils.js", "n2g");

Black
  • 18,150
  • 39
  • 158
  • 271
  • 3
    Its an IIFE. It's the same as `(function(){})()`. https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript – pmkro Jul 24 '18 at 16:32
  • There is no special meaning. It is just a trick into persuading the interpreter that the code that follows it is an IIFE and not just a function definition (`function(){}`) followed by a meaningless list of values in parentheses. – axiac Jul 24 '18 at 16:38
  • 1
    @axiac the values in the trailing parens are not meaningless, they are the arguments to the IIFE. – pmkro Jul 24 '18 at 17:22
  • Thanks! And what is the `!` doing in front of the functions definition? – Black Jul 24 '18 at 18:02
  • @pmkro without `!` it is not an IIFE but a function declaration followed by a list of values in parentheses. The function is not invoked and the value of the entire expression is the value of the last value (sub-expression) in the parentheses. – axiac Jul 25 '18 at 10:31
  • 1
    @Black read my previous comment(s). `!` literally persuade the interpreter to consider the entire block of code as a boolean expression. The value that follows `!` is interpreted as an [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE): a function (usually anonymous) that is declared and invoked in the same statement. An IIFE usually wraps the function's definition into parentheses, otherwise it is not an IIFE. This code is an alternative way to get the same result (that uses less parentheses and could be easier to read). – axiac Jul 25 '18 at 10:35

0 Answers0