> 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/scope/hoist/function/first.md).

# function hoisting first❗️

function hoisting has "higher" priority over variable hoisting.

[JS](/web/js.md) ⟩ [scope](/web/js/scope.md) ⟩ [hoisting](/web/js/scope/hoist.md) ⟩ [function hoisting](/web/js/scope/hoist/function.md) ⟩ first

{% hint style="success" %}
in the same [scope](/web/js/scope.md), [function hoisting](/web/js/scope/hoist/function.md) has <mark style="color:yellow;">**higher priority**</mark> over [variable hoisting](/web/js/scope/hoist/variable.md).
{% endhint %}

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

* replit：[function hoisting first](https://replit.com/@pegasusroe/function-hoisting-has-higher-priority#index.js)

```javascript
const { log } = console;

// <---- greeting === a "function" (at first)

// ⭐️ var hoisting has "lower" priority
var greeting;                    // ignored❗ (function hoisting first)

// ⭐️ function hoisting has "higher" priority
function greeting() {
    console.log("Hello!");
}

log(typeof greeting);            // at first, it's a "function"

// ⭐️ redeclaration ignored, 
// ❗ but assignment applied.
var greeting = "Hello!";        // now greeting is a string❗

log(greeting, typeof greeting); // 'Hello!', "string"❗
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩&#x20;
  * [ ] [Ch. 1](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch1.md#chapter-1-whats-the-scope) ⟩ [Hoisting](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch1.md#hoisting)
  * [ ] Ch. 5 ⟩&#x20;
    * [ ] [Hoisting: Declaration vs. Expression](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch5.md#hoisting-declaration-vs-expression)
    * [ ] [Re-declarations?](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch5.md#re-declaration)
      {% endtab %}
      {% endtabs %}
