LoginSignup
5
6

More than 5 years have passed since last update.

JavaScriptの関数の書き方をまとめる

Posted at

目的

JavaScriptの関数はたくさんの書き方があるので辞書用に列挙する

通常

sample.js
function hoge() {
  console.log("hoge")
}

hoge()
// hoge

無名

sample.js
const hoge = function() {
  console.log("hoge")
}

hoge()
// hoge

即時

sample.js
(function() {
  console.log("hoge")
})()
// hoge

(function(a, b) {
  console.log(a + b)
})(10, 20)
// 30

アロー関数

sample.js
const hoge = () => {
  console.log("hoge")
}

hoge()
// hoge

引数が1つの時は()を省略できる。
関数の中身が1行なら{}を省略できる。

sample.js
const hoge = a => console.log(a);

hoge("hoge")
// hoge

即時アロー関数

sample.js
((a, b) => {
    console.log(a * b);
})(10, 10)

// 100
5
6
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
5
6