2018-09-30 23:01:58 +01:00
---
id: 587d7db2367417b2b2512b8b
title: Understand the Immediately Invoked Function Expression (IIFE)
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 301328
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
A common pattern in JavaScript is to execute a function as soon as it is declared:
2019-05-17 06:20:30 -07:00
```js
(function () {
console.log("Chirp, chirp!");
})(); // this is an anonymous function expression that executes right away
// Outputs "Chirp, chirp!" immediately
```
2019-10-27 15:45:37 -01: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 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
2019-10-27 15:45:37 -01:00
Rewrite the function <code>makeNest</code> and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).
2018-09-30 23:01:58 +01:00
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: The function should be anonymous.
2020-04-20 10:04:28 +01:00
testString: assert(/\((function|\(\))(=>|\(\)){?/.test(code.replace(/\s/g, "")));
2018-10-04 14:37:37 +01:00
- text: Your function should have parentheses at the end of the expression to call it immediately.
2020-09-01 19:48:32 -04:00
testString: assert(/\(.*(\)\(|\}\(\))\)/.test(code.replace(/[\s;]/g, "")));
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function makeNest() {
console.log("A cozy nest is ready");
}
2018-10-08 01:01:53 +01:00
makeNest();
2018-09-30 23:01:58 +01:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
(function () {
console.log("A cozy nest is ready");
})();
```
2020-04-20 10:04:28 +01:00
```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")
)();
```
2018-09-30 23:01:58 +01:00
</section>