---
description: 🚧 施工中
---

# as variable

[JS](../../..) ⟩ [concepts](../../../concept) ⟩ [expression](../../../grammar/statement/expr) ⟩ [function expression]() ⟩ 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 %}
