2018-09-30 22:01:58 +00:00
---
id: 587d7db2367417b2b2512b8b
title: Understand the Immediately Invoked Function Expression (IIFE)
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301328
2021-01-13 02:31:00 +00:00
dashedName: understand-the-immediately-invoked-function-expression-iife
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2018-09-30 22:01:58 +00:00
A common pattern in JavaScript is to execute a function as soon as it is declared:
2019-05-17 13:20:30 +00:00
```js
(function () {
console.log("Chirp, chirp!");
2021-03-03 00:12:12 +00:00
})();
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
This is an anonymous function expression that executes right away, and outputs `Chirp, chirp!` immediately.
2019-10-27 16:45:37 +00:00
Note that the function has no name and is not stored in a variable. The two parentheses () at the end of the function expression cause it to be immediately executed or invoked. This pattern is known as an < dfn > immediately invoked function expression< / dfn > or < dfn > IIFE< / dfn > .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
Rewrite the function `makeNest` and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
The function should be anonymous.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(/\((function|\(\))(=>|\(\)){?/.test(code.replace(/\s/g, '')));
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
Your function should have parentheses at the end of the expression to call it immediately.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(/\(.*(\)\(|\}\(\))\)/.test(code.replace(/[\s;]/g, '')));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
function makeNest() {
console.log("A cozy nest is ready");
}
2018-10-08 00:01:53 +00:00
makeNest();
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
(function () {
console.log("A cozy nest is ready");
})();
```
2020-11-27 18:02:05 +00:00
---
2020-04-20 09:04:28 +00:00
```js
(function () {
console.log("A cozy nest is ready");
}());
```
2020-11-27 18:02:05 +00:00
---
2020-04-20 09:04:28 +00:00
```js
(() => {
console.log("A cozy nest is ready");
})();
```
2020-11-27 18:02:05 +00:00
---
2020-04-20 09:04:28 +00:00
```js
(() =>
console.log("A cozy nest is ready")
)();
```