Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
add-methods-after-inheritance.english.md
change-the-prototype-to-a-new-object.english.md
create-a-basic-javascript-object.english.md
create-a-method-on-an-object.english.md
define-a-constructor-function.english.md
extend-constructors-to-receive-arguments.english.md
inherit-behaviors-from-a-supertype.english.md
iterate-over-all-properties.english.md
make-code-more-reusable-with-the-this-keyword.english.md
override-inherited-methods.english.md
remember-to-set-the-constructor-property-when-changing-the-prototype.english.md
reset-an-inherited-constructor-property.english.md
set-the-childs-prototype-to-an-instance-of-the-parent.english.md
understand-own-properties.english.md
understand-the-constructor-property.english.md
understand-the-immediately-invoked-function-expression-iife.english.md
understand-the-prototype-chain.english.md
understand-where-an-objects-prototype-comes-from.english.md
use-a-constructor-to-create-objects.english.md
use-a-mixin-to-add-common-behavior-between-unrelated-objects.english.md
use-an-iife-to-create-a-module.english.md
use-closure-to-protect-properties-within-an-object-from-being-modified-externally.english.md
use-dot-notation-to-access-the-properties-of-an-object.english.md
use-inheritance-so-you-dont-repeat-yourself.english.md
use-prototype-properties-to-reduce-duplicate-code.english.md
verify-an-objects-constructor-with-instanceof.english.md
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/understand-the-immediately-invoked-function-expression-iife.english.md

89 lines
1.8 KiB
Markdown
Raw Normal View History

---
id: 587d7db2367417b2b2512b8b
title: Understand the Immediately Invoked Function Expression (IIFE)
challengeType: 1
forumTopicId: 301328
---
## Description
<section id='description'>
A common pattern in JavaScript is to execute a function as soon as it is declared:
```js
(function () {
console.log("Chirp, chirp!");
})(); // this is an anonymous function expression that executes right away
// Outputs "Chirp, chirp!" immediately
```
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>.
</section>
## Instructions
<section id='instructions'>
Rewrite the function <code>makeNest</code> and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: The function should be anonymous.
testString: assert(/\((function|\(\))(=>|\(\)){?/.test(code.replace(/\s/g, "")));
- text: Your function should have parentheses at the end of the expression to call it immediately.
testString: assert(/}?\)\(\)$|}\(\)\)$/.test(code.replace(/[\s;]/g, "")));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function makeNest() {
console.log("A cozy nest is ready");
}
makeNest();
```
</div>
</section>
## Solution
<section id='solution'>
```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")
)();
```
</section>