4
2

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 5 years have passed since last update.

JavaScript基礎文法まとめ

4
Last updated at Posted at 2019-03-27

0.背景

Webアプリが作るために、今更ながらJavaScriptの文法をお勉強。
C Sharperなので、違和感ほぼ無し。

1.基本的なJavaScript実行方法

index.html:JavaScriptを適用するHTML
index.html
<!DOCTYPE html>
<html lang="ja">
  ~ headタグ中略 ~
 <body>
    <script src="main.js"></script>     ← ファイル指定
  </body>
</html>

main.js:JavaScriptファイル

main.js
"use strict"                             エラーチェック有効

console.log("Hello JavaScript")

[実行結果の確認 @ Chrome]

① Chromeでデベロッパーツールを起動する。

tool1.png

② Consoleで実行結果を確認する。

tool2.png

2.基本的なJavaScript文法

① コメント記載方法
// 一行コメント

/* 
   複数行コメント
*/

② 正規表現

.       // 改行以外の任意の文字 
\n      // 改行文字 
\r      // キャリッジリターン文字
\t      // タブ文字
\b      // 単語の区切り(単語の開始または終了)
\B      // 単語の区切り以外
\d      // 任意の数字 ( [0-9]と同じ)
\D      // 任意の数字のもの( [^0-9]と同じ)
\s      // シングル空白文字(スペース、タブ、改行など) 
\S      // シングル空白文字以外の文字 
\w      // 単語文字( [A-Za-z0-9_]と同じ)
\W      // 単語文字以外の文字( [^A-Za-z0-9_]と同じ)

③ 関数

main.js
"use strict"

function sum(a, b, c)           つの引数の合計を返す関数を定義
{
  return a+b+c;
}

for (let i = 1; i <= 5; i++)    Forループ
{
  console.log(`${i} + ${i+1} + ${i+2} = ` + sum(i, i+1, i+2));
}

[実行結果]

result.PNG

情報ソース

4
2
1

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?