IIFE Flashcards

1
Q

What does IIFE stand for?

A

Immediately Invoked Function Expression

(function() {
  // IIFE body - code to be executed immediately
})();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an IIFE?

A

An IIFE is executed immediately after it is defined.

(function() {
  // IIFE body - code to be executed immediately
})();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of using an IIFE?

A

It is to create a new scope and encapsulate variables, preventing them from polluting the global scope.

(function() {
  var name = "John";
  console.log("Hello, " + name + "!");
})();
console.log(name); // Throws an error: name is not defined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can an IIFE accept arguments?

A

Yes, an IIFE can accept arguments like any other JavaScript function.

(function(name) {
  console.log("Hello, " + name + "!");
})("Alice");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can the return value of an IIFE be captured?

A

The return value of an IIFE can be captured by assigning the IIFE expression to a variable.

var result = (function() {
  return 42;
})();
console.log(result); // Output: 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of wrapping an IIFE in parentheses?

A

Wrapping an IIFE in parentheses is done to convert the function declaration into a function expression, allowing it to be immediately invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Are IIFEs still commonly used in modern JavaScript?

A

While IIFEs were popular in older JavaScript code, they are less commonly used in modern JavaScript due to the availability of block-scoping with let and const keywords and the use of modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain why the following doesn’t work as an IIFE: function foo(){ }();. What needs to be changed to properly make it an IIFE?

A

IIFE = Immediately Invoked Function Expressions

The JavaScript parser reads function foo(){ }(); as function foo(){ } and ();, where the former is a function declaration and the latter (a pair of parentheses) is an attempt at calling a function but there is no name specified, hence it throws Uncaught SyntaxError: Unexpected token ).

Here are two ways to fix it that involves adding more parentheses: (function foo(){ })() and (function foo(){ }()). Statements that begin with function are considered to be function declarations; by wrapping this function within (), it becomes a function expression which can then be executed with the subsequent (). These functions are not exposed in the global scope and you can even omit its name if you do not need to reference itself within the body.

You might also use void operator: void function foo(){ }();. Unfortunately, there is one issue with such approach. The evaluation of given expression is always undefined, so if your IIFE function returns anything, you can’t use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly