LoginSignup
0

More than 3 years have passed since last update.

[JavaScript][Node.js]メモ:アロー関数の文法

Last updated at Posted at 2020-01-23

アロー関数(ES2015)の書き方。

f = () => console.log('Hello!')
f()  // Hello!
評価した値を返す
// 式(Expressions)の評価結果が戻り値になる
sum = (a, b) => a + b
r = sum(1, 2)
console.log(r) // 3
returnで値を返す
// {}で文(Statements)を作り、returnで値を返す
sum = (a, b) => { 
  return a + b 
}
r = sum(1, 2)
console.log(r) // 3
引数のカッコを省略
// 引数が一つの時のみカッコを省略可能(0個もしくは2つ以上のときは省略不可)
say = word => console.log(word + '!')
say('Yeah')  // Yeah!
即時関数
(() => console.log('Hello!'))() 
引数としてのアロー関数
arr = [1,2,3,4,5]

// かっこよくない
console.log(arr.map(function(e) { return e + 1 }))  // [ 2, 3, 4, 5, 6 ]

// かっこいい
console.log(arr.map(e => e + 1))  // [ 2, 3, 4, 5, 6 ]

リンク(mozilla)

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