LoginSignup
0
0

More than 3 years have passed since last update.

関数宣言と関数式の巻き上げの違いについて

Posted at

巻き上げとは?

今回は関数の巻上げについて解説します。

関数の書き方には関数宣言と関数式があります。
結論から言うと、関数宣言では巻き上げが起こり関数式では巻き上げが起こりません。

関数宣言の場合

function hello() {
console.log(こんにちは)
};

こういった関数宣言の場合、どこでhello();と記述しても、きちんと関数が呼び出されます。
宣言の前に呼び出しを書いてもOKです。これが巻き上げが起こるということです。

関数式の場合

一方、関数そのものを定数に代入する関数式の場合、

const hello = function () {
console.log(こんにちは)
};

console.log(hello);

ではきちんと呼び出されますが、
console.log(hello);を関数式より先に書いてしまってはいけません。Undefined(未定義)となります。

定義する前に定数は使用できませんでしたね。それと同じです。

以上、関数宣言と関数式の違いでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0