You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 10-A_Closer_Look_At_Functions/README.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -400,3 +400,71 @@ const addTask = (rate = 0.23, value) => value + value * rate;
400
400
```
401
401
402
402
But the difference is that the bind method allows us to create a completely different function we can work with.
403
+
404
+
# Immediately Invoked Function Expressions (IIFE)
405
+
406
+
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is called or invoked. This function will only run once
407
+
408
+
```js
409
+
construnOnce=function () {
410
+
console.log(`This will never run again`);
411
+
};
412
+
runOnce(); // Can run again
413
+
```
414
+
415
+
The function above can be called again using the `runOnce` variable. What if we want to call this funtion only within itself, we use the `IIFE`
416
+
417
+
To use the `IIFE`, write a function without a variable name and wrap it inside parethesis(which converts it into an expression), and then we can call it using the parenthesis `()`.
418
+
419
+
```js
420
+
(function () {
421
+
console.log("Hello World");
422
+
})();
423
+
```
424
+
425
+
It also works with arrow fucntions
426
+
427
+
```js
428
+
(() =>console.log("Hello World 2"))();
429
+
```
430
+
431
+
Since we didn't assign a variable on both functions, we can only run it once and there is no way to call it aside within itself.
432
+
433
+
### Importance of IIFE
434
+
435
+
- This **IIFE** is important when we want to handle Async, await operations.
436
+
- Variables defined inside an `IIFE` scope will not be avaialble in the global scope. For example.
437
+
438
+
```js
439
+
(function () {
440
+
console.log("Hello World");
441
+
constnum=23;
442
+
})();
443
+
444
+
console.log(num);
445
+
```
446
+
447
+
This will throw an error because `num` is not accessible in the global scope. Another word for this is called encapsulation.
448
+
449
+
Data encapsulation and Data privacy are extremely important concepts in programming. It allows us to protect variables from being overwritten by external scripts or libraries, and this is primarily the reason why the `IIFE` were introduced in programming.
450
+
451
+
Although it is not a method specific to the JavaScript language but a pattern developers came up with to enable them protect variables.
452
+
453
+
This is also similar to declaring variables using the `const` and `let` keywordswithinacodeblock.
454
+
455
+
```js
456
+
{
457
+
const isPrivate = 23;
458
+
var notPrivate = 22;
459
+
}
460
+
```
461
+
462
+
The `var` keyword points to the global scope and so it will be accessible outside of the code block. While variables declared with`const` and `let` will create their own scope and will not be accessible outside of the code block.
463
+
464
+
In summary thanks to `ES6`, we do not need to use immediatelt invoked functions if all we want is to hide variables, we can simply declare them using `const` and `let`.
465
+
466
+
But if we want to run a function only once, `IIFE` is still the way to go, even now with modern JavaScript.
0 commit comments