LoginSignup
0
0

More than 1 year has passed since last update.

JavaScript 繰り返し処理

Last updated at Posted at 2021-08-13

JavaScriptの学習の備忘録と振り返りの記事です。
何かの参考になれば幸いです。
付け足しや訂正などある場合ご教授いただけると
大変嬉しく思います!!

繰り返し処理とは

ある条件を満たす間に行われる処理を繰り返し行う処理の事

種類

while文・・・条件式がtrueの間、{ }内の処理を繰り返すことができる
for文・・・while文と同じ & while文よりもシンプルに記述できる

書き方

while文
while(条件式){
処理
};セミコロン入れない

値を更新するコードを書き忘れると無限ループになるので忘れないように!! ↓↓↓ while(month <= 12){ console.log(month); 'month += 1; ' ←更新コード }

let month = 1

while(month <= 12){
  console.log(month);
   month += 1;
}

出力結果
1
2
3
4
5
6
7
8
9
10
11
12

for文
for(変数の定義 ; 条件式 ; 変数の更新){
処理
};セミコロン入れない

変数の更新は省略して書くことができます。 例「number += 1」 ⇨ 「number ++」  「number -= 1」 ⇨ 「number --」

for(let month = 1 ; month <= 12 ; month++){
   console.log(month);
}

出力結果
1
2
3
4
5
6
7
8
9
10
11
12

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