---
tags: JS, beginner
---
# Hoisting 函式
...move the declaration of functions, variables or classes to **the top of their scope**, prior to execution of the code. -JS 裡 變數和函式皆會 Hoisting,
***
```htmlmixed=
// 函式 Hoisting
function hi(name) {
console.log(`hi ${name}`);
}
hi('Tom'); // hi Tom
age(16); // 16 old
function age(years) {
console.log(`${years} old`);
}
console.log(gender);
gender('male'); // gender is not a function
var gender = function (sex) {
console.log(sex);
}
```
function 經 Hoisting, 執行方法 ( age(16) 或 hi('Tom') ) 寫在 function 的前後,都可以執行!
***
## 一旦 function 作為一變數值; 賦值給變數 (如 14 行),在 13 行顯示 **gender is not a function**
### function 作為一變數值,需注意定義變數的位置 ((變數 Hoisting
將執行方法 gender('male') 稍作調整,便可以得到輸出 male
```htmlmixed=
var gender = function (sex) {
console.log(sex);
}
console.log(gender); // f(sex)..印出函式內容
gender('male'); // male
```
***
[Hoisting](/npBWp0qbQJS4H3oOqBbVPg)