0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】アロー関数の簡素化とうっかりミス

Last updated at Posted at 2025-07-12

JavaScriptの関数

JavaScript(JS)の関数の書き方は関数宣言関数式アロー関数の3種類あります。

1.関数宣言

func.js
function func(x) { //function 関数名(引数)
    return x+5; //x+5をreturnで返す
}

console.log(func(5)); //コンソールに10が表示される

2.関数式

関数名を宣言しないので無名関数とも呼ばれる

func.js
const func = function(x){ //const 変数名 = function(引数)
    return x+5; //x+5をreturnで返す
}

console.log(func(5)); //コンソールに10が表示される

3.アロー関数

func.js
const func = (x) =>{ //const 変数名 = (引数) =>
    return x+5; //x+5をreturnで返す
}

console.log(func(5)); //コンソールに10が表示される

アロー関数の簡素化

引数の()の省略

引数が1つの場合、引数の()は省略できる

func.js
const func = x =>{ //const 変数名 = 引数 =>
    return x+5; //x+5をreturnで返す
}
console.log(func(5)); //コンソールに10が表示される

処理の{}、returnの省略

処理が単一式である場合、{}とreturnを省略できる

func.js
const func = (x) =>x+5 //const 変数名 = 引数 =>x+5を返す

console.log(func(5)); //コンソールに10が表示される

うっかりミス(実体験)

{} を記述しているのに returnは記述していない => エラーになる

func.js
const func = x =>{ //const 変数名 = 引数 =>
    x+5; //x+5を"return"で返す
}
console.log(func(5)); //エラーになる
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?