> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/func/expr/as-variable.md).

# as variable

[JS](/web/js.md) ⟩ [concepts](/web/js/concept.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [function expression](/web/js/val/func/expr.md) ⟩ as variable

{% tabs %}
{% tab title="💈範例" %}

* replit：[assign function expression to variable](https://replit.com/@pegasusroe/assign-function-expression-to-variable#index.js)

```javascript
// ⭐️ the variable to which the "function expression" is assigned:
// -----------------------------------------------------------------------------
//   • will have a `name` property
//   • the name won't change if it's assigned to a different variable.
//   • (implicit name) if function is anonymous, it will be the "variable name".
//   • (explicit name) if function is named, it will be the "function name".

// ⭐️ (anonymous) function expression
//        ╭──── ⭐️ ────╮
const f = function() { }; 
f.name                   // "f" (implicit name = variable name)

const f2 = f;
f2.name;                 // "f" (the `name` won't change❗)

// ⭐️ arrow function
//        ╭─ ⭐️ ─╮
const h = () => {};
h.name;                  // "h" (implicit name also applies to arrow functions)

// ⭐️ (named) function expression
//        ╭────── ⭐️ ───────╮
const g = function game() { };
g.name;                  // "game" (explicit name = function name)

typeof g;       // 'function'
typeof game;    // undefined (`game` is only scoped within the function body❗)
```

{% endtab %}

{% tab title="📘 手冊" %}

* [ ] [named function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#named_function_expression)
  {% endtab %}
  {% endtabs %}
