最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - while(1) in JavaScript - Stack Overflow

programmeradmin7浏览0评论

I'm converting some es5 into es6 and using eslint and e across the following

while(1){
  // Do something
}

I'm getting a lint error

Unexpected constant condition no-constant-condition

But I'm not sure exactly what this does. It resides inside a for loop

for(i = 0; i< arr.length; i++) {
  while(1) {
    idx = _p.indexOf(arr[i], idx);
    if (idx == -1) break;
  }
}

But there was no documentation and unable to find online what exactly while(1) does and how to write an alternative to get rid of this lint error.

I'm converting some es5 into es6 and using eslint and e across the following

while(1){
  // Do something
}

I'm getting a lint error

Unexpected constant condition no-constant-condition

But I'm not sure exactly what this does. It resides inside a for loop

for(i = 0; i< arr.length; i++) {
  while(1) {
    idx = _p.indexOf(arr[i], idx);
    if (idx == -1) break;
  }
}

But there was no documentation and unable to find online what exactly while(1) does and how to write an alternative to get rid of this lint error.

Share Improve this question asked Nov 3, 2017 at 11:06 user6885115user6885115 3
  • while(1) will continuously run, until you break out of it. It's a short hand for DO-WHILE. So it says, while the idx does not equal -1, always run this loop. – Dan Commented Nov 3, 2017 at 11:08
  • while(1) is just another dirty approach for continuity – B001ᛦ Commented Nov 3, 2017 at 11:09
  • Possible duplicate of Why does ESLint trigger lint errors on while(true) using fibers? – user8016859 Commented Nov 3, 2017 at 11:37
Add a ment  | 

5 Answers 5

Reset to default 2

You could rewrite the code with a do ... while loop with a check at the end, because that is what you do.

for (i = 0; i< arr.length; i++) {
    do {
        idx = _p.indexOf(arr[i], idx);
    } while (idx !== -1)
}

A Loop

Instructions are repeated n times.

While Loop

while needs a condition (a boolean value) to handle a loop. If the condition is true it executes else it does nothing.

In this case the condition is 1 and gets cast to a true.

function getBooleanValue(value) {
  return !!value
}

console.log('1 gets cast to:', getBooleanValue(1))
console.log('0 gets cast to:', getBooleanValue(0))

Infinite Loop

A loop that never terminates.

Your loop is after the cast while(true). With other words do for ever.

Why this Rule

From the Docs:

A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production.

if (false) {
    doSomethingUnfinished();
}

if you want to disable it: /*eslint-disable no-constant-condition*/

while(1) creates a loop that runs forever (never terminates) unless you explicitly stop it with the break keyword. The loop will also stop if an error occurs during it's execution. These constructs can be very dangerous (crash your pc/browser) and that is why jslint warns about them.

An other way of writing this would be while(true) or using for(;;) (https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for).

Link to docs

This rule disallows constant expressions in the test condition of: if, for, while, or do...while statement ?: ternary expression

Examples of incorrect code for this rule:

if (false) {
    doSomethingUnfinished();
}

if (void x) {
    doSomethingUnfinished();
}

for (;-2;) {
    doSomethingForever();
}

while (typeof x) {
    doSomethingForever();
}

do {
    doSomethingForever();
} while (x = -1);

var result = 0 ? a : b;

Do: while(true)

Or you can just disable that: /*eslint-disable no-constant-condition*/

That's simply an infinite loop. It cycles until 1 isn't true (never). As you can see, inside this loop there's a condition and a break, so the infinite loop will end when the condition is true, hence the break.

while(1) { //Cycle forever...
    idx = ...;
    if (idx == -1) break; //...until idx == -1!
}
发布评论

评论列表(0)

  1. 暂无评论