Immediately invoked function expression without using grouping operator





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







20















I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios:




  1. When a function declaration is invoked immediately: gives SyntaxError.


  2. When a function expression is invoked immediately: executes successfully.



Example 1: gives SyntaxError






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





Example 2: Executes without any error






// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function





So, I have these doubts:




  • With this approach, why does it give an error for function declaration but not for function expression?

  • Is there a way we can immediately invoke a function declaration without using IIFE pattern?










share|improve this question




















  • 3





    Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

    – Rajesh
    May 13 at 8:38






  • 1





    +function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

    – Aaditya Sharma
    May 13 at 8:59






  • 5





    "does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

    – Amadan
    May 13 at 9:07








  • 3





    @AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

    – VLAZ
    May 13 at 9:56






  • 1





    see: developer.mozilla.org/en-US/docs/Glossary/IIFE

    – Matt
    May 13 at 14:01


















20















I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios:




  1. When a function declaration is invoked immediately: gives SyntaxError.


  2. When a function expression is invoked immediately: executes successfully.



Example 1: gives SyntaxError






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





Example 2: Executes without any error






// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function





So, I have these doubts:




  • With this approach, why does it give an error for function declaration but not for function expression?

  • Is there a way we can immediately invoke a function declaration without using IIFE pattern?










share|improve this question




















  • 3





    Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

    – Rajesh
    May 13 at 8:38






  • 1





    +function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

    – Aaditya Sharma
    May 13 at 8:59






  • 5





    "does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

    – Amadan
    May 13 at 9:07








  • 3





    @AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

    – VLAZ
    May 13 at 9:56






  • 1





    see: developer.mozilla.org/en-US/docs/Glossary/IIFE

    – Matt
    May 13 at 14:01














20












20








20


7






I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios:




  1. When a function declaration is invoked immediately: gives SyntaxError.


  2. When a function expression is invoked immediately: executes successfully.



Example 1: gives SyntaxError






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





Example 2: Executes without any error






// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function





So, I have these doubts:




  • With this approach, why does it give an error for function declaration but not for function expression?

  • Is there a way we can immediately invoke a function declaration without using IIFE pattern?










share|improve this question
















I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios:




  1. When a function declaration is invoked immediately: gives SyntaxError.


  2. When a function expression is invoked immediately: executes successfully.



Example 1: gives SyntaxError






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





Example 2: Executes without any error






// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function





So, I have these doubts:




  • With this approach, why does it give an error for function declaration but not for function expression?

  • Is there a way we can immediately invoke a function declaration without using IIFE pattern?






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function





// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function






javascript function iife






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 13 at 20:49









Bergi

389k64604930




389k64604930










asked May 13 at 8:28









Aaditya SharmaAaditya Sharma

431319




431319








  • 3





    Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

    – Rajesh
    May 13 at 8:38






  • 1





    +function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

    – Aaditya Sharma
    May 13 at 8:59






  • 5





    "does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

    – Amadan
    May 13 at 9:07








  • 3





    @AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

    – VLAZ
    May 13 at 9:56






  • 1





    see: developer.mozilla.org/en-US/docs/Glossary/IIFE

    – Matt
    May 13 at 14:01














  • 3





    Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

    – Rajesh
    May 13 at 8:38






  • 1





    +function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

    – Aaditya Sharma
    May 13 at 8:59






  • 5





    "does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

    – Amadan
    May 13 at 9:07








  • 3





    @AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

    – VLAZ
    May 13 at 9:56






  • 1





    see: developer.mozilla.org/en-US/docs/Glossary/IIFE

    – Matt
    May 13 at 14:01








3




3





Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

– Rajesh
May 13 at 8:38





Quick question. IIFE invokes code immediately and creates a scope. So what do you expect with immediately invoke a function declaration without using IIFE pattern

– Rajesh
May 13 at 8:38




1




1





+function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

– Aaditya Sharma
May 13 at 8:59





+function(){console.log('done')}() and new function(){console.log('done')}() seem to work. I wonder, what's going under the hood here.

– Aaditya Sharma
May 13 at 8:59




5




5





"does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

– Amadan
May 13 at 9:07







"does JavaScript provides any ways for us to immediately invoke a function declaration without wrapping them inside parentheses?" - Yes - it only needs to disambiguate that it is function expression and not a function declaration, which can be done by e.g. prefixing with any operators (e.g. !function() { .... }). "why does it give an error for function declaration but not for function expression?" Have you noticed what IIFE means? "Immediately invoked function expression." It is not IIFD/E. Function invocation is an expression - and you cannot use a statement inside an expression.

– Amadan
May 13 at 9:07






3




3





@AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

– VLAZ
May 13 at 9:56





@AadityaSharma what is an "IIFE pattern"? IIFE means an immediately invoked function expression. If you have a function expression and invoke it immediately, that's an IIFE. Your second example is exactly that - a function expression which you invoke. Same with +function(){} although it's more hacky - it merely forces this to be evaluates as an expression instead of declaration.

– VLAZ
May 13 at 9:56




1




1





see: developer.mozilla.org/en-US/docs/Glossary/IIFE

– Matt
May 13 at 14:01





see: developer.mozilla.org/en-US/docs/Glossary/IIFE

– Matt
May 13 at 14:01












6 Answers
6






active

oldest

votes


















13














In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.






function func(){
console.log('x')
}();





The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.



In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.




Is there a way we can immediately invoke a function declaration without using IIFE pattern




You can use + which will make function declaration an expression.






+function(){
console.log('done')
}()





If you don't want to use + and () you can use new keyword






new function(){
console.log('done')
}





Extra



A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases



+function(){} //returns NaN
(+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
+function(){return 5}() //5



+function(){} returns NaN



+ acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN




(+function(){return 5;})() returns Error



Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as



Number(function(){})()



+function(){return 5;}() returns 5



In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as



Number(function(){return 5}())


In the proof of statement "+ runs on after the function is called" Consider the below snippet






console.log(typeof +function(){return '5'}());





So in the above snippet you can see the returned value is string '5' but is converted to number because of +






share|improve this answer





















  • 2





    ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

    – Ilmari Karonen
    May 13 at 9:46








  • 2





    Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

    – cat
    May 13 at 14:14








  • 2





    @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

    – Maheer Ali
    May 13 at 14:44











  • @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

    – Aaditya Sharma
    May 13 at 18:01











  • @AadityaSharma Yes absolutely.

    – Maheer Ali
    May 13 at 18:53



















14














A function declaration, like



function foo() {
}


defines (and hoists) the variable name foo as a function in the current scope. A function declaration doesn't evaluate to a value; it just does something, a bit like an if does something (rather than evaluate to a value).



You can only invoke values which are functions, eg



<somevalue>()


where somevalue is a variable name that refers to a function.



Note that function declarations require function names, because otherwise there's no variable name to assign the function to - your original






//gives `SyntaxError`
function() {
console.log('Inside the function');
}();





throws not because of the () at the end, but because of the lack of a name.



You can put parentheses at the end of a function declaration, as long as there's something inside the parentheses - but these parentheses do not call the function, they evaluate to a value:






function x() {
console.log('Inside the function');
}(console.log('some expression inside parentheses'));





The interpreter sees these as two separate statements, like






function x() {
console.log('Inside the function');
}
// completely separate:
(console.log('some expression inside parentheses'));





The inside of the parentheses gets evaluated and then discarded, since it doesn't get assigned to anything.



(Empty parentheses are forbidden because they can't be evaluated to a value, similar to how const foo = () is forbidden)






share|improve this answer


























  • Might help to have the last example without () to show it throws.

    – Kaiido
    May 13 at 8:35











  • It gives an error with a named function too function kaka() {console.log('Inside the function');}();

    – Aaditya Sharma
    May 13 at 8:36













  • @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

    – Aaditya Sharma
    May 13 at 8:38






  • 1





    Your answer lacks ,any alternative to invoke function immediately except iife.

    – Shubh
    May 13 at 8:39











  • @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

    – CertainPerformance
    May 13 at 8:39



















2














The E in IIFE stands for expression, and without the wrapping parenthesis your function is not evaluated as an expression thus the syntax error.



creating an expression is a way of tricking the interpreter and be able to invoke the function immediatly




(function() {
console.log('Inside the function');
})();





In your example you have a function statement followed by the grouping operator, but it's syntactically incorrect for two reasons, first it doesn't have a name, and second because the grouping operator must have an expression inside it, infact if you add a valid one the error will disappear, still you won't obtain your desired result.






function foo() {
console.log('Inside the function');
}();








function foo() {
console.log('Inside the function');
}(1+2);








share|improve this answer

































    1














    In order to invoke something, it has to be a function value, a declaration just declares a name, and does not evaluate to the function value itself, hence you cannot invoke it.



    A declaration cannot be invoked for the above reason. You have to end up with an expression somehow, either through assignment or grouping (IIFE). So that is a no.



    If you give us more context on why you would want to do that, maybe we can help with suggestions.






    share|improve this answer































      1














      Not sure why you would want to do it, but:




      Is there a way we can immediately invoke a function declaration without using IIFE pattern?




      Well, if for function declaration you mean using the keyword function as in:



      function name() { return this.name; }


      As far as I know, no. You need the extra parentheses to tell the runtime not to assign the function to a name variable, if I understand this stuff right.



      Now, what you actually can do is to use Function as in:



      new Function('console.log("ey there")')();


      Which will execute the console.log code. No need for IIFE here. But I don't see how this could be better than an IIFE.






      share|improve this answer































        0














        you can call in either below ways -



        ~function(){console.log("hi")}()
        !function(){console.log("hi")}()
        +function(){console.log("hi")}()
        -function(){console.log("hi")}()
        (function(){console.log("hi")}());
        var i = function(){console.log("hi")}();
        true && function(){ console.log("hi") }();
        0, function(){ console.log("hi") }();
        new function(){ console.log("hi") }
        new function(){ console.log("hi") }()





        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56108429%2fimmediately-invoked-function-expression-without-using-grouping-operator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          13














          In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.






          function func(){
          console.log('x')
          }();





          The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.



          In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.




          Is there a way we can immediately invoke a function declaration without using IIFE pattern




          You can use + which will make function declaration an expression.






          +function(){
          console.log('done')
          }()





          If you don't want to use + and () you can use new keyword






          new function(){
          console.log('done')
          }





          Extra



          A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases



          +function(){} //returns NaN
          (+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
          +function(){return 5}() //5



          +function(){} returns NaN



          + acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN




          (+function(){return 5;})() returns Error



          Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as



          Number(function(){})()



          +function(){return 5;}() returns 5



          In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as



          Number(function(){return 5}())


          In the proof of statement "+ runs on after the function is called" Consider the below snippet






          console.log(typeof +function(){return '5'}());





          So in the above snippet you can see the returned value is string '5' but is converted to number because of +






          share|improve this answer





















          • 2





            ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

            – Ilmari Karonen
            May 13 at 9:46








          • 2





            Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

            – cat
            May 13 at 14:14








          • 2





            @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

            – Maheer Ali
            May 13 at 14:44











          • @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

            – Aaditya Sharma
            May 13 at 18:01











          • @AadityaSharma Yes absolutely.

            – Maheer Ali
            May 13 at 18:53
















          13














          In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.






          function func(){
          console.log('x')
          }();





          The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.



          In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.




          Is there a way we can immediately invoke a function declaration without using IIFE pattern




          You can use + which will make function declaration an expression.






          +function(){
          console.log('done')
          }()





          If you don't want to use + and () you can use new keyword






          new function(){
          console.log('done')
          }





          Extra



          A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases



          +function(){} //returns NaN
          (+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
          +function(){return 5}() //5



          +function(){} returns NaN



          + acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN




          (+function(){return 5;})() returns Error



          Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as



          Number(function(){})()



          +function(){return 5;}() returns 5



          In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as



          Number(function(){return 5}())


          In the proof of statement "+ runs on after the function is called" Consider the below snippet






          console.log(typeof +function(){return '5'}());





          So in the above snippet you can see the returned value is string '5' but is converted to number because of +






          share|improve this answer





















          • 2





            ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

            – Ilmari Karonen
            May 13 at 9:46








          • 2





            Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

            – cat
            May 13 at 14:14








          • 2





            @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

            – Maheer Ali
            May 13 at 14:44











          • @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

            – Aaditya Sharma
            May 13 at 18:01











          • @AadityaSharma Yes absolutely.

            – Maheer Ali
            May 13 at 18:53














          13












          13








          13







          In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.






          function func(){
          console.log('x')
          }();





          The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.



          In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.




          Is there a way we can immediately invoke a function declaration without using IIFE pattern




          You can use + which will make function declaration an expression.






          +function(){
          console.log('done')
          }()





          If you don't want to use + and () you can use new keyword






          new function(){
          console.log('done')
          }





          Extra



          A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases



          +function(){} //returns NaN
          (+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
          +function(){return 5}() //5



          +function(){} returns NaN



          + acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN




          (+function(){return 5;})() returns Error



          Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as



          Number(function(){})()



          +function(){return 5;}() returns 5



          In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as



          Number(function(){return 5}())


          In the proof of statement "+ runs on after the function is called" Consider the below snippet






          console.log(typeof +function(){return '5'}());





          So in the above snippet you can see the returned value is string '5' but is converted to number because of +






          share|improve this answer















          In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.






          function func(){
          console.log('x')
          }();





          The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.



          In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.




          Is there a way we can immediately invoke a function declaration without using IIFE pattern




          You can use + which will make function declaration an expression.






          +function(){
          console.log('done')
          }()





          If you don't want to use + and () you can use new keyword






          new function(){
          console.log('done')
          }





          Extra



          A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases



          +function(){} //returns NaN
          (+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
          +function(){return 5}() //5



          +function(){} returns NaN



          + acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN




          (+function(){return 5;})() returns Error



          Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as



          Number(function(){})()



          +function(){return 5;}() returns 5



          In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as



          Number(function(){return 5}())


          In the proof of statement "+ runs on after the function is called" Consider the below snippet






          console.log(typeof +function(){return '5'}());





          So in the above snippet you can see the returned value is string '5' but is converted to number because of +






          function func(){
          console.log('x')
          }();





          function func(){
          console.log('x')
          }();





          +function(){
          console.log('done')
          }()





          +function(){
          console.log('done')
          }()





          new function(){
          console.log('done')
          }





          new function(){
          console.log('done')
          }





          console.log(typeof +function(){return '5'}());





          console.log(typeof +function(){return '5'}());






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 13 at 14:50

























          answered May 13 at 8:39









          Maheer AliMaheer Ali

          20k31836




          20k31836








          • 2





            ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

            – Ilmari Karonen
            May 13 at 9:46








          • 2





            Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

            – cat
            May 13 at 14:14








          • 2





            @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

            – Maheer Ali
            May 13 at 14:44











          • @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

            – Aaditya Sharma
            May 13 at 18:01











          • @AadityaSharma Yes absolutely.

            – Maheer Ali
            May 13 at 18:53














          • 2





            ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

            – Ilmari Karonen
            May 13 at 9:46








          • 2





            Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

            – cat
            May 13 at 14:14








          • 2





            @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

            – Maheer Ali
            May 13 at 14:44











          • @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

            – Aaditya Sharma
            May 13 at 18:01











          • @AadityaSharma Yes absolutely.

            – Maheer Ali
            May 13 at 18:53








          2




          2





          ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

          – Ilmari Karonen
          May 13 at 9:46







          ...but note that using new like that will have the side effect of constructing an object using the provided function as its constructor (and then immediately garbage collecting it, since you don't save the returned value). Try e.g. let foo = new function() { console.log("in function") }; console.log(foo); console.log(foo.constructor); to see what I mean.

          – Ilmari Karonen
          May 13 at 9:46






          2




          2





          Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

          – cat
          May 13 at 14:14







          Can you say why or how +function(){} is (correctly) NaN, and (+function(){ return 5 })() is (+ (intermediate value)) is not a function, yet +function(){ return 5 }() is 5?

          – cat
          May 13 at 14:14






          2




          2





          @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

          – Maheer Ali
          May 13 at 14:44





          @cat Your question worth to be answered in original answers rather than comments. I have added a detailed answer. Check it out. If any confusion ask me

          – Maheer Ali
          May 13 at 14:44













          @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

          – Aaditya Sharma
          May 13 at 18:01





          @MaheerAli this was helpful - In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide.

          – Aaditya Sharma
          May 13 at 18:01













          @AadityaSharma Yes absolutely.

          – Maheer Ali
          May 13 at 18:53





          @AadityaSharma Yes absolutely.

          – Maheer Ali
          May 13 at 18:53













          14














          A function declaration, like



          function foo() {
          }


          defines (and hoists) the variable name foo as a function in the current scope. A function declaration doesn't evaluate to a value; it just does something, a bit like an if does something (rather than evaluate to a value).



          You can only invoke values which are functions, eg



          <somevalue>()


          where somevalue is a variable name that refers to a function.



          Note that function declarations require function names, because otherwise there's no variable name to assign the function to - your original






          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          throws not because of the () at the end, but because of the lack of a name.



          You can put parentheses at the end of a function declaration, as long as there's something inside the parentheses - but these parentheses do not call the function, they evaluate to a value:






          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          The interpreter sees these as two separate statements, like






          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));





          The inside of the parentheses gets evaluated and then discarded, since it doesn't get assigned to anything.



          (Empty parentheses are forbidden because they can't be evaluated to a value, similar to how const foo = () is forbidden)






          share|improve this answer


























          • Might help to have the last example without () to show it throws.

            – Kaiido
            May 13 at 8:35











          • It gives an error with a named function too function kaka() {console.log('Inside the function');}();

            – Aaditya Sharma
            May 13 at 8:36













          • @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

            – Aaditya Sharma
            May 13 at 8:38






          • 1





            Your answer lacks ,any alternative to invoke function immediately except iife.

            – Shubh
            May 13 at 8:39











          • @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

            – CertainPerformance
            May 13 at 8:39
















          14














          A function declaration, like



          function foo() {
          }


          defines (and hoists) the variable name foo as a function in the current scope. A function declaration doesn't evaluate to a value; it just does something, a bit like an if does something (rather than evaluate to a value).



          You can only invoke values which are functions, eg



          <somevalue>()


          where somevalue is a variable name that refers to a function.



          Note that function declarations require function names, because otherwise there's no variable name to assign the function to - your original






          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          throws not because of the () at the end, but because of the lack of a name.



          You can put parentheses at the end of a function declaration, as long as there's something inside the parentheses - but these parentheses do not call the function, they evaluate to a value:






          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          The interpreter sees these as two separate statements, like






          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));





          The inside of the parentheses gets evaluated and then discarded, since it doesn't get assigned to anything.



          (Empty parentheses are forbidden because they can't be evaluated to a value, similar to how const foo = () is forbidden)






          share|improve this answer


























          • Might help to have the last example without () to show it throws.

            – Kaiido
            May 13 at 8:35











          • It gives an error with a named function too function kaka() {console.log('Inside the function');}();

            – Aaditya Sharma
            May 13 at 8:36













          • @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

            – Aaditya Sharma
            May 13 at 8:38






          • 1





            Your answer lacks ,any alternative to invoke function immediately except iife.

            – Shubh
            May 13 at 8:39











          • @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

            – CertainPerformance
            May 13 at 8:39














          14












          14








          14







          A function declaration, like



          function foo() {
          }


          defines (and hoists) the variable name foo as a function in the current scope. A function declaration doesn't evaluate to a value; it just does something, a bit like an if does something (rather than evaluate to a value).



          You can only invoke values which are functions, eg



          <somevalue>()


          where somevalue is a variable name that refers to a function.



          Note that function declarations require function names, because otherwise there's no variable name to assign the function to - your original






          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          throws not because of the () at the end, but because of the lack of a name.



          You can put parentheses at the end of a function declaration, as long as there's something inside the parentheses - but these parentheses do not call the function, they evaluate to a value:






          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          The interpreter sees these as two separate statements, like






          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));





          The inside of the parentheses gets evaluated and then discarded, since it doesn't get assigned to anything.



          (Empty parentheses are forbidden because they can't be evaluated to a value, similar to how const foo = () is forbidden)






          share|improve this answer















          A function declaration, like



          function foo() {
          }


          defines (and hoists) the variable name foo as a function in the current scope. A function declaration doesn't evaluate to a value; it just does something, a bit like an if does something (rather than evaluate to a value).



          You can only invoke values which are functions, eg



          <somevalue>()


          where somevalue is a variable name that refers to a function.



          Note that function declarations require function names, because otherwise there's no variable name to assign the function to - your original






          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          throws not because of the () at the end, but because of the lack of a name.



          You can put parentheses at the end of a function declaration, as long as there's something inside the parentheses - but these parentheses do not call the function, they evaluate to a value:






          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          The interpreter sees these as two separate statements, like






          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));





          The inside of the parentheses gets evaluated and then discarded, since it doesn't get assigned to anything.



          (Empty parentheses are forbidden because they can't be evaluated to a value, similar to how const foo = () is forbidden)






          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          //gives `SyntaxError`
          function() {
          console.log('Inside the function');
          }();





          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          function x() {
          console.log('Inside the function');
          }(console.log('some expression inside parentheses'));





          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));





          function x() {
          console.log('Inside the function');
          }
          // completely separate:
          (console.log('some expression inside parentheses'));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 13 at 8:37

























          answered May 13 at 8:34









          CertainPerformanceCertainPerformance

          109k1672101




          109k1672101













          • Might help to have the last example without () to show it throws.

            – Kaiido
            May 13 at 8:35











          • It gives an error with a named function too function kaka() {console.log('Inside the function');}();

            – Aaditya Sharma
            May 13 at 8:36













          • @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

            – Aaditya Sharma
            May 13 at 8:38






          • 1





            Your answer lacks ,any alternative to invoke function immediately except iife.

            – Shubh
            May 13 at 8:39











          • @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

            – CertainPerformance
            May 13 at 8:39



















          • Might help to have the last example without () to show it throws.

            – Kaiido
            May 13 at 8:35











          • It gives an error with a named function too function kaka() {console.log('Inside the function');}();

            – Aaditya Sharma
            May 13 at 8:36













          • @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

            – Aaditya Sharma
            May 13 at 8:38






          • 1





            Your answer lacks ,any alternative to invoke function immediately except iife.

            – Shubh
            May 13 at 8:39











          • @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

            – CertainPerformance
            May 13 at 8:39

















          Might help to have the last example without () to show it throws.

          – Kaiido
          May 13 at 8:35





          Might help to have the last example without () to show it throws.

          – Kaiido
          May 13 at 8:35













          It gives an error with a named function too function kaka() {console.log('Inside the function');}();

          – Aaditya Sharma
          May 13 at 8:36







          It gives an error with a named function too function kaka() {console.log('Inside the function');}();

          – Aaditya Sharma
          May 13 at 8:36















          @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

          – Aaditya Sharma
          May 13 at 8:38





          @CertainPerformance - This example just executed whats there inside the last set of paratheses, ignoring the function declaration. (console.log('some expression inside parentheses'));

          – Aaditya Sharma
          May 13 at 8:38




          1




          1





          Your answer lacks ,any alternative to invoke function immediately except iife.

          – Shubh
          May 13 at 8:39





          Your answer lacks ,any alternative to invoke function immediately except iife.

          – Shubh
          May 13 at 8:39













          @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

          – CertainPerformance
          May 13 at 8:39





          @AadityaSharma The function declaration is not ignored - the variable name x gets assigned (and hoisted) to that function in the current scope. But x is never called, so you don't see Inside the function.

          – CertainPerformance
          May 13 at 8:39











          2














          The E in IIFE stands for expression, and without the wrapping parenthesis your function is not evaluated as an expression thus the syntax error.



          creating an expression is a way of tricking the interpreter and be able to invoke the function immediatly




          (function() {
          console.log('Inside the function');
          })();





          In your example you have a function statement followed by the grouping operator, but it's syntactically incorrect for two reasons, first it doesn't have a name, and second because the grouping operator must have an expression inside it, infact if you add a valid one the error will disappear, still you won't obtain your desired result.






          function foo() {
          console.log('Inside the function');
          }();








          function foo() {
          console.log('Inside the function');
          }(1+2);








          share|improve this answer






























            2














            The E in IIFE stands for expression, and without the wrapping parenthesis your function is not evaluated as an expression thus the syntax error.



            creating an expression is a way of tricking the interpreter and be able to invoke the function immediatly




            (function() {
            console.log('Inside the function');
            })();





            In your example you have a function statement followed by the grouping operator, but it's syntactically incorrect for two reasons, first it doesn't have a name, and second because the grouping operator must have an expression inside it, infact if you add a valid one the error will disappear, still you won't obtain your desired result.






            function foo() {
            console.log('Inside the function');
            }();








            function foo() {
            console.log('Inside the function');
            }(1+2);








            share|improve this answer




























              2












              2








              2







              The E in IIFE stands for expression, and without the wrapping parenthesis your function is not evaluated as an expression thus the syntax error.



              creating an expression is a way of tricking the interpreter and be able to invoke the function immediatly




              (function() {
              console.log('Inside the function');
              })();





              In your example you have a function statement followed by the grouping operator, but it's syntactically incorrect for two reasons, first it doesn't have a name, and second because the grouping operator must have an expression inside it, infact if you add a valid one the error will disappear, still you won't obtain your desired result.






              function foo() {
              console.log('Inside the function');
              }();








              function foo() {
              console.log('Inside the function');
              }(1+2);








              share|improve this answer















              The E in IIFE stands for expression, and without the wrapping parenthesis your function is not evaluated as an expression thus the syntax error.



              creating an expression is a way of tricking the interpreter and be able to invoke the function immediatly




              (function() {
              console.log('Inside the function');
              })();





              In your example you have a function statement followed by the grouping operator, but it's syntactically incorrect for two reasons, first it doesn't have a name, and second because the grouping operator must have an expression inside it, infact if you add a valid one the error will disappear, still you won't obtain your desired result.






              function foo() {
              console.log('Inside the function');
              }();








              function foo() {
              console.log('Inside the function');
              }(1+2);








              (function() {
              console.log('Inside the function');
              })();





              (function() {
              console.log('Inside the function');
              })();





              function foo() {
              console.log('Inside the function');
              }();





              function foo() {
              console.log('Inside the function');
              }();





              function foo() {
              console.log('Inside the function');
              }(1+2);





              function foo() {
              console.log('Inside the function');
              }(1+2);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 14 at 9:03

























              answered May 13 at 8:42









              KarimKarim

              5,7651826




              5,7651826























                  1














                  In order to invoke something, it has to be a function value, a declaration just declares a name, and does not evaluate to the function value itself, hence you cannot invoke it.



                  A declaration cannot be invoked for the above reason. You have to end up with an expression somehow, either through assignment or grouping (IIFE). So that is a no.



                  If you give us more context on why you would want to do that, maybe we can help with suggestions.






                  share|improve this answer




























                    1














                    In order to invoke something, it has to be a function value, a declaration just declares a name, and does not evaluate to the function value itself, hence you cannot invoke it.



                    A declaration cannot be invoked for the above reason. You have to end up with an expression somehow, either through assignment or grouping (IIFE). So that is a no.



                    If you give us more context on why you would want to do that, maybe we can help with suggestions.






                    share|improve this answer


























                      1












                      1








                      1







                      In order to invoke something, it has to be a function value, a declaration just declares a name, and does not evaluate to the function value itself, hence you cannot invoke it.



                      A declaration cannot be invoked for the above reason. You have to end up with an expression somehow, either through assignment or grouping (IIFE). So that is a no.



                      If you give us more context on why you would want to do that, maybe we can help with suggestions.






                      share|improve this answer













                      In order to invoke something, it has to be a function value, a declaration just declares a name, and does not evaluate to the function value itself, hence you cannot invoke it.



                      A declaration cannot be invoked for the above reason. You have to end up with an expression somehow, either through assignment or grouping (IIFE). So that is a no.



                      If you give us more context on why you would want to do that, maybe we can help with suggestions.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 13 at 8:36









                      Slawomir ChodnickiSlawomir Chodnicki

                      1,194514




                      1,194514























                          1














                          Not sure why you would want to do it, but:




                          Is there a way we can immediately invoke a function declaration without using IIFE pattern?




                          Well, if for function declaration you mean using the keyword function as in:



                          function name() { return this.name; }


                          As far as I know, no. You need the extra parentheses to tell the runtime not to assign the function to a name variable, if I understand this stuff right.



                          Now, what you actually can do is to use Function as in:



                          new Function('console.log("ey there")')();


                          Which will execute the console.log code. No need for IIFE here. But I don't see how this could be better than an IIFE.






                          share|improve this answer




























                            1














                            Not sure why you would want to do it, but:




                            Is there a way we can immediately invoke a function declaration without using IIFE pattern?




                            Well, if for function declaration you mean using the keyword function as in:



                            function name() { return this.name; }


                            As far as I know, no. You need the extra parentheses to tell the runtime not to assign the function to a name variable, if I understand this stuff right.



                            Now, what you actually can do is to use Function as in:



                            new Function('console.log("ey there")')();


                            Which will execute the console.log code. No need for IIFE here. But I don't see how this could be better than an IIFE.






                            share|improve this answer


























                              1












                              1








                              1







                              Not sure why you would want to do it, but:




                              Is there a way we can immediately invoke a function declaration without using IIFE pattern?




                              Well, if for function declaration you mean using the keyword function as in:



                              function name() { return this.name; }


                              As far as I know, no. You need the extra parentheses to tell the runtime not to assign the function to a name variable, if I understand this stuff right.



                              Now, what you actually can do is to use Function as in:



                              new Function('console.log("ey there")')();


                              Which will execute the console.log code. No need for IIFE here. But I don't see how this could be better than an IIFE.






                              share|improve this answer













                              Not sure why you would want to do it, but:




                              Is there a way we can immediately invoke a function declaration without using IIFE pattern?




                              Well, if for function declaration you mean using the keyword function as in:



                              function name() { return this.name; }


                              As far as I know, no. You need the extra parentheses to tell the runtime not to assign the function to a name variable, if I understand this stuff right.



                              Now, what you actually can do is to use Function as in:



                              new Function('console.log("ey there")')();


                              Which will execute the console.log code. No need for IIFE here. But I don't see how this could be better than an IIFE.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 13 at 8:59









                              SergeonSergeon

                              3,428920




                              3,428920























                                  0














                                  you can call in either below ways -



                                  ~function(){console.log("hi")}()
                                  !function(){console.log("hi")}()
                                  +function(){console.log("hi")}()
                                  -function(){console.log("hi")}()
                                  (function(){console.log("hi")}());
                                  var i = function(){console.log("hi")}();
                                  true && function(){ console.log("hi") }();
                                  0, function(){ console.log("hi") }();
                                  new function(){ console.log("hi") }
                                  new function(){ console.log("hi") }()





                                  share|improve this answer




























                                    0














                                    you can call in either below ways -



                                    ~function(){console.log("hi")}()
                                    !function(){console.log("hi")}()
                                    +function(){console.log("hi")}()
                                    -function(){console.log("hi")}()
                                    (function(){console.log("hi")}());
                                    var i = function(){console.log("hi")}();
                                    true && function(){ console.log("hi") }();
                                    0, function(){ console.log("hi") }();
                                    new function(){ console.log("hi") }
                                    new function(){ console.log("hi") }()





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      you can call in either below ways -



                                      ~function(){console.log("hi")}()
                                      !function(){console.log("hi")}()
                                      +function(){console.log("hi")}()
                                      -function(){console.log("hi")}()
                                      (function(){console.log("hi")}());
                                      var i = function(){console.log("hi")}();
                                      true && function(){ console.log("hi") }();
                                      0, function(){ console.log("hi") }();
                                      new function(){ console.log("hi") }
                                      new function(){ console.log("hi") }()





                                      share|improve this answer













                                      you can call in either below ways -



                                      ~function(){console.log("hi")}()
                                      !function(){console.log("hi")}()
                                      +function(){console.log("hi")}()
                                      -function(){console.log("hi")}()
                                      (function(){console.log("hi")}());
                                      var i = function(){console.log("hi")}();
                                      true && function(){ console.log("hi") }();
                                      0, function(){ console.log("hi") }();
                                      new function(){ console.log("hi") }
                                      new function(){ console.log("hi") }()






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 23 at 16:27









                                      Samyak JainSamyak Jain

                                      3991311




                                      3991311






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56108429%2fimmediately-invoked-function-expression-without-using-grouping-operator%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

                                          Bunad

                                          Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum