LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptの基礎~変数宣言、if、switch、while、forなど

Last updated at Posted at 2019-07-10

JavaScriptの基本

コピペして使ってください。
ES6を対象としています。

変数・定数とテンプレートリテラル

sample.js
//コメント

//コンソール出力
console.log("Hello World");

//変数の宣言(再宣言、再代入可)
let name = "John";
//定数の宣言(再宣言、再代不可)
const name = "John";

//テンプレートリテラル(文字列と変数の連結)
const name = "John";
console.log(`こんにちは、${name}さん`);

if文

sample.js
//if文
const age = 12;
if (age >= 20) {
  console.log("私は20歳以上です");
}  else {
  console.log("私は20歳未満です");
}
//aとbが等しい
console.log(age===12);
//aとbが異なる
console.log(age!==12);

switch文

:sample.js
const rank = 5;

switch (rank) {
  case 1:
    console.log("金メダルです!");
    break;
  case 2:
    console.log("銀メダルです!");
    break;
  case 3:
    console.log("銅メダルです!");
    break;
  default:
    console.log("メダルはありません");
    break;    

}

while文

sample.js
let number = 1;

while (number<=100){
  console.log(number);
  number += 1;
}

for文

sample.js
for (let number = 1; number <= 100; number ++){
  console.log(number);
}
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