yiddish.ninja
Menu
  • About
  • Bedtime Stories
Menu

JavaScript Infinite Loops

Posted on February 19, 2018February 21, 2018 by Greg

I was thinking “programming is easy, but good programming is hard.” As a way of demonstrating programming is easy, I remembered the second program I learned to write after “Hello World.”

The Hello World Loop (in BASIC)
MS DOS
1
2
10 PRINT "HELLO WORLD."
20 GOTO 10

I asked myself how I would implement that simple infinite loop in JavaScript, then realized there are a bunch of ways to create an infinite loop.

The Conditionless For

The Conditionless For
JavaScript
1
2
3
4
for(;;)
    {
        console.log('Hello World.');
    }

The Unchanging Condition:

The Unchanging Condition
JavaScript
1
2
3
4
while(1<2)
    {
        console.log("Hello World.");
    }

Infinite Recursion:

Infinite Recursion
JavaScript
1
2
3
4
5
function helloWorld()
    {
        console.log('Hello World.');
        helloWorld();
    }

The For Loop Reset:

For Loop Reset
JavaScript
1
2
3
4
5
for(var i = 0; i<2; i++)
    {
        console.log("Hello World.");
        i = 0;
    }

60 Hello Worlds Per Second (or slower infinite recursion):

60 Hello Worlds Per Second
JavaScript
1
2
3
4
5
function helloWorld()
    {
        console.log("Hello World");
        var repeat = window.requestAnimationFrame(helloWorld);
    }

Note: This would also work with setTimeout() or setInterval().

Global Condition, Local Change (a variant on The Unchanging Condition)

Advance the Counter in the Wrong Scope
JavaScript
1
2
3
4
5
6
7
8
9
var iterator = 0;
while (iterator < 5){
console.log("Hello World");
increment();
}
function increment(){
var iterator; // creates local var
iterator++;
}

That’s all I can come up with off the top of my head. If you can think of one I missed, please add it in the comments (without <script></script> tags, please)

Update (2/21/18)

Complementary Recursion:

Complementary Recursion
JavaScript
1
2
3
4
5
6
7
8
9
10
function helloWorld()
    {
        console.log('Hello World.');
        helloUniverse();
    }
function helloUniverse()
    {
        console.log('Hello Universe.');
        helloWorld();
    }

The Self-Invoking Recursive Hello:

Self-Invoking Recursive Hello
JavaScript
1
2
3
4
(function hello() {
  console.log("Hello World");
  hello();
}());

Callback Hello (a variation on Complementary Recursion):

Callback Hello
JavaScript
1
2
3
4
5
6
7
function hello() {
  console.log("hello world");
  comeback(hello);
}
function comeback(dacaller) {
    dacaller();
}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • What is Programming?
  • Becoming a Raspberry Pi Certified Educator
  • MOD Pizza’s Cauliflower Crust
  • Keeping my resolutions
  • Pain and Python

Archives

Categories

Disclaimer

The views and opinions expressed on this site are my own and do not represent any organizations, projects. or businesses with which I am involved as an employee, member, participant, or leader.

Recent Comments

  • Ron on Lowe’s: A Case Study in Bad Customer Service
  • Greg on Viewing Someone’s Previous LinkedIn Posts
  • Abhiram Venkitela on Viewing Someone’s Previous LinkedIn Posts
  • Pikachu1902 on Sharing Your Local Minecraft Server Over the Internet
  • John MARTIN on Mags for your Canik TP40

No Warranty

I'm human, I'm imperfect, and I make mistakes. There is no guarantee of accuracy, serviceability, functionality, or applicability of anything I write or link to here. By viewing this site, you agree that you are solely responsible for all outcomes related to that act and release me from any liability.
©2019 yiddish.ninja | WordPress Theme by Superb Themes