4

This question born considering the function declarations VS function expressions.

We clearly know that a function declaration have this form

function foo() { 
    var a = 3;
    console.log( a );
}

while a function expression can have this form (appearing like what is known like Immediately invoked function expression)

(function foo() { 
    var a = 3;
    console.log( a );
})()

Looking at the immediately invoked function expression I can just notice the first function (the one used in the function declaration) wrapped in parethesis.

Now, the point is: I know that the grouping operator (most commonly known as "the parethesis" () ) can only contain an expression. If this is true I can't say that the function declaration is also a function statement (because it would be like saying that I have wrapped in parenthesis a statement...).

So, can you give some help, I'm a little confused. A function declaration is also a function statement? If yes, the function wrapped in parenthesis is a statement or what?

I must clarify that I'm not asking about the difference beetwen a function declaration and a function expression. If I mention them and their differences is just to describe my question that is: a "function declaration" is also a "function statement"?

marco
  • 1,152
  • 1
  • 9
  • 20
  • 1
    Many expressions are also statements. – Code-Apprentice Nov 14 '17 at 17:23
  • 1
    "A function expression is very similar to and has almost the same syntax as a function statement (see function statement for details). The main difference between a function expression and a function statement is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as a IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined. " - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function – zfrisch Nov 14 '17 at 17:25
  • Possible duplicate of [What is the difference between an expression and a statement?](https://stackoverflow.com/questions/32343679/what-is-the-difference-between-an-expression-and-a-statement) – zfrisch Nov 14 '17 at 17:28
  • @zfrisch I'm not talking about the difference beetwen a function declaration and a function expression! Don't loose your time suspecting of possible duplicates... Just read the title of this question and you can see that my question is different – marco Nov 14 '17 at 17:30
  • It's worth noting that the named IIFE's name only exists inside that IIFE, and can't be called by that name outside. – Niet the Dark Absol Nov 14 '17 at 17:39

2 Answers2

3

While the syntax of a function expression is the same as a function declaration, they're not the same thing.

The scope of the name used in function declarations is different from named function expressions. When you write:

function foo() { 
    var a = 3;
    console.log( a );
}

the scope of foo is the enclosing function (or the global environment if this is at top level).

When you write:

(function foo() { 
    var a = 3;
    console.log( a );
})

the scope of foo is just the body of the function.

The parser determines whether it's a function expression or declaration based on the context. Since a statement can't be inside parentheses, putting parentheses around it forces it to be an expression.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

In expression hoisting will not happened, but in function statement hoisting is occurred.

In function statement, You can write function definition anywhere in js file before or after function call. JIT compiler will hoisting at top of programming file.

For function expression, JIT compiler will not do as same as function statement, which reads where its occurred. If you try to access function before its defined it will show type error.

IIFE is pattern mostly used to avoid access variable outside function.

callMe();  // Function statement

function callMe(){

return console.log("I am function statement");
}


// callMe2();  // TypeError if you call before expression defined.

var callMe2 = function(){
  return console.log("I am function expression");
};

callMe2(); //Function expression call

(function(){
return console.log("I am IIFE");
}());