javascriptfunctionself-executing-function

Why Self-Executing Anonymous works?


I know about Self-Executing Anonymous. And usually we create them as

(function(){ return 1;})()

the reason - parser feature which didn't run if we use

function(){ return 1}()

But today I found that next code works too ( check brackets order )

(function(){ return 1;}())

function(){ return 1; }() still give me SyntaxError, as it should

Please explain why? Thx for reference to get more details

P.S. the question is about (function(){ return 1;}()) variant!


Solution

  • (function() {})()
    

    and

    (function() {}())
    

    are equivalent.

    To call second example you can include + operator before function

    +function(){ return 1 }()
    

    See Immediately-Invoked Function Expression (IIFE)