> For the complete documentation index, see [llms.txt](https://april.gitbook.io/learning-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://april.gitbook.io/learning-js/chapter-7./7.7.md).

# 7.7 즉시 호출하는 함수 표현식

* 함수를 선언하고 즉시 실행함
* 스코프 안에서 안전하게 보호되며 외부에서 접근할 수 없음
* 함수이기때문에 무엇이든 반환 가능
* 배열, 객체, 함수를 반환하는 경우가 많음

### 즉시 호출하는 함수 표현식 (*IIFE*, Immediately Invoked Function Expression)

함수 표현식으로 익명 함수를 만들고 그 함수를 즉시 호출함

```javascript
(function() {
    // IIFE 바디
})();
```

### 내부 변수를 외부에서 접근하지 못하는 것을 보여주는 예제

```javascript
const f = (function() {
    let count = 0;
    return function() {
        return 'I have been called ${++count} time(s).';
    }
})();

f(); // 'I have been called 1 time(s).'
f(); // 'I have been called 2 time(s).'
```

변수 count는 IIFE안에 안전하게 보관되어 있음 -> **접근 방법 X**
