0
0

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

【TypeScript/JavaScript】一定条件になるまでループする

Last updated at Posted at 2020-07-26

for (;;) + break 版

let num = 0

for (;;) { 
  num += 1
  const ok = num >= 10
  if (ok) break
}

console.log(num) // => 10

do ... while 版

let num = 0
let ok = true

do {
  num += 1
  ok = num < 10
} while (ok)

console.log(num) // => 10

or

let num = 0
let ok = false

do {
  num += 1
  ok = num >= 10
} while (!ok)

console.log(num) // => 10

参考

ループと反復処理

備考

while (true) + break は、以下の eslint のルールに抵触するため断念。
https://eslint.org/docs/rules/no-constant-condition

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?