Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are parentheses required around JavaScript IIFE? [duplicate]

I'm reading up on JavaScript IIFE and so far the understand concept, but I am wondering about the outside parenthesis. Specifically, why are they required? For example,

(function() {var msg='I love JavaScript'; console.log(msg);}());

works great, but

function() {var msg='I love JavaScript'; console.log(msg);}();

generates a syntax error. Why? There are lots of discussions on IIFE, but I'm not seeing a clear explanation about why the parentheses are required.

like image 931
buttonsrtoys Avatar asked Aug 10 '15 00:08

buttonsrtoys


People also ask

Why do we use parentheses in JavaScript?

Description. The grouping operator consists of a pair of parentheses around an expression or sub-expression to override the normal operator precedence so that operators with lower precedence can be evaluated before an operator with higher precedence.

What is the main advantage of using IIFE closures for individual scripts when using multiple scripts like on the Scription website?

Advantages of IIFE: Do not create unnecessary global variables and functions. Functions and variables defined in IIFE do not conflict with other functions & variables even if they have same name. Organize JavaScript code.

What does IIFE mean in JavaScript?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

Why is IIFE important?

An IIFE is a good way to secure the scope. You can use IIFEs to prevent global variables' definition issues, alias variables, protect private data, and avoid conflicts of using many libraries that export the same object name.


1 Answers

There are two ways to create functions in JavaScript (well, 3, but let's ignore new Function()). You can either write a function declaration or write a function expression.

A function declaration in itself is a statement and statements by themselves don't return values (let's also ignore how the debugging console or Node.js REPL print return values of statements). A function expression however is a proper expression and expressions in JavaScript returns values that can be immediately used.

Now, you may have seen people saying that the following is a function expression:

var x = function () {};

It may be tempting to conclude that the syntax:

function () {};

is what makes it an expression. But that's wrong. The syntax above is what makes it an anonymous function. And anonymous functions can either be a declaration or an expression. What makes it an expression is this syntax:

var x = ...

That is, everything to the right of an = sign is an expression. Expressions make it easier to write math formulas in programming languages. So in general everywhere that math is expected to be processed is an expression.

Some of the forms of expressions in JavaScript include:

  • everything to the right of an = operator
  • things in braces () that are not function call braces
  • everything to the right of a math operator (+,-,*,/)
  • all the arguments to the ternary operator .. ? .. : ..

When you write:

function () {}

it is a declaration and does not return a value (the declared function). Therefore trying to call the non-result is an error.

But when you write:

(function () {})

it is an expression and returns a value (the declared function) which may be used immediately (for example, may be called or may be assigned).

Note the rules for what counts as expressions above. From that it follows that braces are not the only things that you can use to construct an IIFE. Below are valid ways for constructing IIFEs (because we write function expressions):

tmp=function(){}()

+function(){}()

-function(){}()

0/function(){}()

0*function(){}()

0?0:function(){}()

(function(){}())

(function(){})()

You may actually see one of the above non-standard forms (particularly the + version) in third-party libraries, because they want to save one byte. But I strongly advise you to only use the brace forms (either are fine), because they are widely recognized as IIFEs by other programmers.

like image 110
slebetman Avatar answered Oct 18 '22 21:10

slebetman