> For the complete documentation index, see [llms.txt](https://strctr.gitbook.io/programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://strctr.gitbook.io/programming/01-languages/javascript/01-language/c-functions/c1-functions/01.ii-function-expression.md).

# 01.ii Function Expression

**Function Expression** - объявление функции в контексте какого-либо выражения, например присваивания.

```javascript
// Function Declaration
function sum(a, b) {
  return a + b;
}

// Function Expression
const sum = function(a, b) {
  return a + b;
};

// Arrow funciton
const sum = (a, b) => a + b;
```

Основные особенности:

* функция всегда находится в зоне выражения;
* имеет опциональное имя;
* не воздействует на объект переменных;
* и создается на этапе интерпретации кода контекста.
* такую функцию нельзя вызвать до того, как интерпретатор дойдет до выполнения выражения

Специально для работы с рекурсией в JavaScript существует особое расширение функциональных выражений, которое называется *Named Function Expression* (сокращённо `NFE`) или «именованное функциональное выражение». Особенность `NFE` в том, что она может рекурсивно вызывать себя по своему локальному имени:

```javascript
const func = function sayHi(...) {
    /* тело функции */
    return 1 + sayHi(...)
};
```

Предпочитайте использовать `NFE`:

* Легче читать сообщения об ошибке.
* Легче отлаживать
* Имя функции позволяет понять что она делает
* Можно делать рекурсивные вызовы этой функции

В отличие от function declaration, function expression всегда находится в зоне выражения:

```javascript
// в скобках (оператор группировки) может быть только выражение
(function foo() {});

// в инициализаторе массива - всегда выражение
[function bar() {}];

// запятая также оперирует выражениями
1, function alsoFE() {};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://strctr.gitbook.io/programming/01-languages/javascript/01-language/c-functions/c1-functions/01.ii-function-expression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
