2018-10-10 22:03:03 +00:00
---
id: 587d7db2367417b2b2512b8b
2021-02-06 04:42:36 +00:00
title: Understand the Immediately Invoked Function Expression (IIFE)
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-08-04 07:15:28 +00:00
forumTopicId: 301328
2021-01-13 02:31:00 +00:00
dashedName: understand-the-immediately-invoked-function-expression-iife
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
A common pattern in JavaScript is to execute a function as soon as it is declared:
2020-08-04 07:15:28 +00:00
```js
(function () {
console.log("Chirp, chirp!");
2021-02-06 04:42:36 +00:00
})(); // this is an anonymous function expression that executes right away
// Outputs "Chirp, chirp!" immediately
2020-08-04 07:15:28 +00:00
```
2021-02-06 04:42:36 +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-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Rewrite the function `makeNest` and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The function should be anonymous.
2018-10-10 22:03:03 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(/\((function|\(\))(=>|\(\)){?/.test(code.replace(/\s/g, '')));
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
Your function should have parentheses at the end of the expression to call it immediately.
2020-08-04 07:15:28 +00:00
2018-10-10 22:03:03 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(/\(.*(\)\(|\}\(\))\)/.test(code.replace(/[\s;]/g, '')));
2018-10-10 22:03:03 +00:00
```
2020-08-04 07:15:28 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
function makeNest() {
console.log("A cozy nest is ready");
}
makeNest();
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
(function () {
console.log("A cozy nest is ready");
})();
```
---
```js
(function () {
console.log("A cozy nest is ready");
}());
```
---
```js
(() => {
console.log("A cozy nest is ready");
})();
```
---
```js
(() =>
console.log("A cozy nest is ready")
)();
```