1
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?

More than 3 years have passed since last update.

色々な関数の書き方 【JavaScipt】

Last updated at Posted at 2020-09-02

###どれも結果は同じなのに、関数の記法は実に色々。
###混乱していた所、とても良い教材と巡り合った。 以下、ざっと一覧。


  //関数宣言
  function double(x) {
    return x * 2;
  };

  //関数式
  const double = function (x) {
    return x * 2;
  };

  //アロー関数 function 省略
  const double = (x) => {
    return x * 2;
  };

  //アロー関数 引数が1つのときは()省略
  const double = x => {
    return x * 2;
  };

  //アロー関数 {}省略
  const double = x => x * 2;

###アレンジ--引数が2つver.

  //=======三角形の面積=============//

    //  (底辺 × 高さ / 2) 

  function triangle(a, b) {//関数宣言
    return a * b / 2;
  }
  
  const triangle = function (a, b) {//関数式
    return a * b / 2; 
  }
  
  const triangle = (a, b) => {  //アロー関数 functionを省略
    return a * b / 2; 
  } 
  
  const triangle = (a, b) => { //引数が2つなので、()は要
    return a * b / 2;
  }
  
  const triangle = (a, b) => a * b / 2;//1行のときは、波括弧{}省略
  
  console.log(triangle(3, 4));  // => 全て「6」

###ピタゴラス(三平方の定理)

  //====== ピタゴラス ===========///

  //  a2乗 + b2乗 = c2乗 //
  
  function PythagoreanTheorem(a, b,) {
    return a** 2 + b** 2 
  }
    console.log('C = ' + PythagoreanTheorem(3, 4)); // => C = 25
####尚、どれを使うかは、開発現場によるとのこと。

[参考:しまぶーのIT大学] https://www.instagram.com/p/CAHEsTsDg2U/

1
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
1
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?