Infinite loop in JavaScript

  • Post author:
  • Post last modified:02/10/2024
  • Reading time:9 mins read
Infinite loop in JavaScript

In JavaScript, an infinite loop is a loop that never terminates unless the browser or environment forcibly stops it. There are multiple ways to create an infinite loop. Here’s an example using a while loop:

JavaScript
while (true) {
  // Code to run indefinitely
  console.log("This will run forever!");
}

Alternatively, you can create an infinite loop using a for loop:

JavaScript
for (;;) {
  // Code to run indefinitely
  console.log("This will also run forever!");
}

In both cases, the condition always evaluates to true, and the loops will continue to execute endlessly.

PC Prajapat

Hi, I'm PC Prajapat, the CEO and founder of codebypc, an online platform dedicated to helping aspiring developers master coding skills. With 2 years of hands-on experience as a MERN stack web developer, I bring real-world insights into every course I teach. Based in Pune, I am currently balancing my full-time job with my passion for teaching coding, guiding students through the journey of becoming proficient developers. My mission is to make coding accessible, understandable, and enjoyable for everyone.

Leave a Reply