LoginSignup
0
0

More than 3 years have passed since last update.

【JavaScript】基礎的な文法編

Last updated at Posted at 2020-09-29

編集中です!

・typeof
文字列の型を表示できる

console.log(typeof 11); # number

・NaN
Not a Number(ナンと呼ぶ)。数値が表せないよ〜ってこと。

・文字列の評価
- false(falseと表示されるもの)
- 0
- null
- undifined
- 「’’」(空文字)
- false
- true
- 上記以外全てtrue

・論理演算子「!a」
NOT演算のこと。〜ではない。「a以外なら何でもおk」。rubyのunlessに似てる。
何か前にも躓いた気がする。

      a = 1;
      console.log(!(a === 0));
    # true

      a = 1;
      console.log(!(a === 1));
    # false

・switch
まんまrubyのwhenですね。

test.js
      a = 1;
      switch (a) {
        case 0:
          console.log(0);
        case 1:
          console.log(1);
        default:
          console.log("aaa");
      }
// 出力結果: 1

・テンプレートリテラル
ES6から使えるようになったやつ。
バッククオート「`」で範囲を囲む!
変数や計算式を入れられる。

`aaa`
${1+1 + a}

・デフォルト引数
メソッドのデフォルトで表示する引数を決められる。値ありなしでもエラー起きないよ。

      function showAd(message = "Ad") {
        console.log(`--- ${message} ---`);
      }
      showAd("Ad");
      showAd();

// 出力結果
—- Ad ---
—- Ad ---

・ブロックで囲うとエラーが出ない(ブロックスコープ)
⬇️下記はエラーが出る(定数がすでに宣言されてる!エラー)

index.html
  <script src="js/main.js"></script>
  <script>
      const x = 300;
      console.log(x);
  </script>
main.js
  const x = 100;
  console.log(x);

⬇️下記はエラーが出ません(定数が別々で定義されているから)

index.html
  <script src="js/main.js"></script>
  <script>
    {
      const x = 300;
      console.log(x);
    }
  </script>
main.js
{
  const x = 100;
  console.log(x);
}
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