Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Why is no error thrown for infinite loops

This is my code snippet

const loopFn = function(num) {

  for(let a = 0; a < num || 10; a++) { 
    console.log(a)
  }

}

let b = loopFn(8)

console.log(b)

I know the correct way to write is a < (num || 10), I'm just curious why it causes an infinite loop instead of reporting an error.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

What you mean do to is

const count = num || 10;

for(let a = 0; a < count; a++) { 
    console.log(a)
}

But what you're actually doing is saying while a is less than 8 OR 10 is true

about 4 years ago · Juan Pablo Isaza Report

0

The language/execution engine does not report it as an error because there is no inherent issue with infinite loops itself.

Depending upon the use-case infinite loops are of help. For instance, in daemon scripts that are meant to process jobs from a queue.

about 4 years ago · Juan Pablo Isaza Report

0

The condition of your for loop is:

a < num || 10

If either a < num is true, or 10 is true the loop continues.

Try in your browser console, and you will see, that 10 is always true. if(10) alert("true")

If you want the loop to end if a is not < 10, try this:

for(let a = 0; a < num || a < 10; a++) 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!