Are function names stored inside of an IIFE?

iife es6
immediately invoked function expression es6
iife react
iife with parameters
iife typescript
iife function
why use iife
what is iife immediately invoked function expression and how it can be useful

It is my understanding that the grouping operator around a function declaration forces the function to be evaluated as an expression. This is what allows the execution parentheses operator () to work. However, this approach removes the function name from being accessible outside itself. I'm wondering how IIFE function names are stored compared to function declaration names which are available in the scope they are declared.

(function hidden(){
    console.log("function executed");
})()

There's a good article by Angus Croll about the difference between a function declaration and a function expression in javascript (https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/)

Basically what you're trying to do is treat the function hidden() as a function declaration inside of a function expression. As Croll states in the article, inside a function expression, "the function name (if any) is not visible outside of it’s scope (contrast with Function Declarations)."

If you rewrite the anonymous function as an assignment it makes the point clearer I think:

var a = function hidden() {
  console.log('inside hidden');
}

now this will error:

var a = function hidden() {
  console.log('inside hidden');
}
hidden();

because the name of the function is not available outside of its own context.

This however will work fine:

var a = function hidden() {
  console.log('inside hidden');
}
a();

since the variable can be referenced outside of its own context as you would expect (otherwise it wouldn't be reachable anywhere but inside the hidden() function body).

If we take a look again at the anonymous version you can see why it fails:

(function hidden() {
  console.log('inside hidden');
}
// We're outside of IIFEs function body here
// so NO code can be executed.
)();

javascript - Are function names stored inside of an IIFE?, However, this approach removes the function name from being accessible outside itself. I'm wondering how IIFE function names are stored compared to function� IIFE stands for Immediately-invoked Function Expression. It’s a JavaScript function that runs as soon as it’s defined. It has no name and is not stored in a variable. These are commonly seen in…

Because it's simply a parsing/syntax error, as you said, "unexpected identifier". You could fix this by using the comma operator: (function hidden() { ... }, hidden()), but the hidden name (holding the function object) from your function expression will be only available inside the function scope. You could solve this by doing a definition instead:

(function() {
    function hidden(){
        console.log("hidden");
    }
    hidden();
})();

You could also do bound definitions. Like...

ES4 (reference interpreter builds):

{
    let function hidden()
    {
        console.log("hidden");
    }
    hidden();
}

ES6-based JavaScript

{
    let hidden = function()
    {
        console.log("hidden");
    }
    hidden();
}

Immediately Invoked Function Expressions (IIFEs) Explained, You can store the IIFE in a variable, since function expressions evaluate to a (function) value — whereas function declarations, being statements, don't evaluate to anything. An Immediately-invoked Function Expression (IIFE for friends) is a way to execute functions immediately, as soon as they are created.. IIFEs are very useful because they don’t pollute the global object, and they are a simple way to isolate variables declarations.

The name of a function is available by accessing the function's name property:

IIFE:

(function hidden() {
    console.log("Function name is: '" + hidden.name + "'"); //Function name is: 'hidden'
})();
console.log(hidden.name);//Error because IIFEs have their own private scope. 

function declaration:

function available(){
    return;
}
console.log(available.name);//"Available" - the console.log has access to the function's scope.

Essential JavaScript: Mastering Immediately-invoked Function , Understanding functions inside out and then learning how to exploit them to Or more dearly known as IIFE and pronounced as “iffy. So the difference here is that the function expression has a name “fibonacci” that can be used inside that A button that says 'Download on the App Store', and if clicked it. But fortunately the concept itself is simple. The pattern is called an immediately invoked function expression, or IIFE (pronounced "iffy"). In JavaScript functions can be created either through a function declaration or a function expression. A function declaration is the "normal" way of creating a named function.

On the other hand the following is perfectly valid:

(two = function() {
  console.log('two');
}, 
console.log('one'), 
two)()

IIFE, Assigning the IIFE to a variable stores the function's return value, not the function definition itself. var result = (function () { var name = "Barry";� IIFE in JavaScript Immediately Invoked Function Expression (IIFE) is one of the most popular design patterns in JavaScript. As name suggest, IIFE is a function expression that automatically invokes after completion of the definition. The parenthesis plays important role in IIFE pattern.

You have a syntax error because

(function hidden(){
    console.log("hidden");
}
hidden();)

is not an expression but

(function hidden(){
    console.log("hidden");
})

is an expression that returns the function itself and you call it with ().

You could make it an expression by adding a comma between the two parts, and removing the semi-colon. However, hidden would be only defined within the function itself and you couldn't call it. That's called a named function expression

// Uncaught ReferenceError: hidden is not defined
(function hidden(){
    console.log("hidden");
},
hidden())

You probably meant to do the following

(function() {
    function hidden(){
        console.log("hidden");
    }
    hidden();
})()

In that case, you are creating a function declaration that is visible within the containing IIFE

IIFE in JavaScript: What Are Immediately Invoked Function , A function created with a function declaration is a Function object and that function name can be omitted to create anonymous functions. In the below example variable iife will store a string that is returned by the function execution. This ensures that code inside IIFE does not interfere with other code� An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. (function { statements })(); It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts: The first is the anonymous function with lexical scope enclosed within the Grouping Operator

Immediately Invoked Function Expressions (IIFE), If you give your function a name, you can re-use those grouped statements a whole bunch of Because the code you put inside an IIFE is actually inside a function, any That detail is that Closures store their outer values by referencing them. Anonymous Functions. An anonymous function is a function that is anonymous. It doesn’t have a name. However, it can be stored in a variable and run essentially the same way.

Immediately invoked function expression, An immediately invoked function expression (or IIFE, pronounced "iffy") is a JavaScript A lack of block scope means that variables defined inside (for example) a for introduced the current term IIFE as a more semantically accurate name for the Random article � About Wikipedia � Contact us � Donate � Wikipedia store� These are Anonymous Function that is wrapped inside a set of paranthesis and is called immediately. The common advantage of IIFE is that any "Function or Variable" defined inside IIFE, cannot be accessed outside the IIFE block, thus preventing global scope from getting polluted. Also helps us manage memory in an efficient manner.

Demystifying JavaScript Closures, Callbacks and IIFEs, Point 3: Inner functions store their outer function's variables by actually placed inside that function, they are in practice closures: they As the name suggests, an IIFE must be a function expression, not a function definition.

Comments
  • What are you trying to achieve with the second piece of code?
  • "there is an error which states that hidden is an unexpected identifier." --- nope, the error message does not say that.
  • your second code block is not valid at all - and if you moved hidden(); inside the {} you'd end up with too much recursion
  • if, on the other hand you moved hidden() outside the IIFE, then, of course, hidden is undefined, due to the scope of the named function expression - but the final () invokes the IIFE anyway, so why do you need to invoke it a second time?
  • Named function declarations are visible only inside of the function itself. The following works (function a() { console.log(a()); return 'a'})
  • I'm not sure this is correct, if you called the code where the comment is, wouldn't that be invalid? I would have thought the whole lot would need to be wrapped in yet another function? otherwise (expression)() does nothing?
  • @RyanTheLeach yep I think we're both saying the same thing? Where that comment is in the last code snippet, you cannot execute the hidden() function.
  • I wouldn't think you can execute anything that didn't have a function as a return type?
  • The point @RyanTheLeach is making is valid, you cannot execute anything at that point (no matter if hidden would be defined elsewhere in the code), it would simply lead to the syntax error described by the OP. But for the rest, it's a good explanation
  • Last answer's point is kinda correct, but no, // so NO code can be executed note is wrong. The parenthesis expression (or grouping operator) matches an enclosed expression which can be any of the language expressions, i.e., the comma operator (also called "sequence expression"), which consists of two expressions. Then, if a comma appears after the }, then it's the comma operator. An + token may also appear, among many other things, i.e. (function a(){} >>> 5)
  • Well, you're creating a global variable. -1 Yeah, you're making it an expression instead of a declaration, if you add better explanation, I'll remove the downvote.