LoginSignup
0
3

More than 5 years have passed since last update.

アロー関数についての色々

Last updated at Posted at 2019-03-25

基本形

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

hoge()
//=> hoge

引数を使う

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

hoge('return value')
//=> return value

()の省略

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

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

hoge('return value')
//=> return value

引数が2つ以上の場合は()を省略することができない

sample.js
const hoge = value1, value2 => {
  console.log(value1);
  console.log(value2);
};

hoge('return value1', 'return value2')
//=> SyntaxError



const hoge = (value1, value2) => {
  console.log(value1);
  console.log(value2);
};

hoge('return value1', 'return value2')
//=> return value1
//=> return value2

returnを使う

sample.js
const hoge = () => {
  return 'hoge';
};

console.log(hoge());
//=> hoge

{}の省略

returnを使用しない場合は{}を省略することができる

sample.js
const hoge = () => 'hoge';

console.log(hoge());
//=> hoge

1行だけで書ける実行であれば下記のように書ける

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

hoge()
//=> hoge
0
3
2

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
3