LoginSignup
0
0

More than 3 years have passed since last update.

JavaScript while文

Posted at

 javascriptのwhile文の書き方です。

while(条件式) {
    繰り返し実行する処理。条件式がtrueの間繰り返される
}

 while文では条件式がtrueの間、繰り返し処理が実行されます。

  let i = 1;
  while(i <= 10) {
    console.log(i);
    i += 1;
  }

 このような書き方をすると、変数iが10以下の場合に繰り返し処理が実行されることになります。
console.log(i)でコンソールに数字が表示された後、i += 1;で変数iを再定義しています。繰り返されるごとに変数iに+1される形です。変数iが11になると条件式がfalseになるので、条件式がtrueになる10回目の繰り返しまでで処理は終了になります。

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