JavaScript Infinite Loops

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.”

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 Unchanging Condition:

Infinite Recursion:

The For Loop Reset:

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

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

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

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:

The Self-Invoking Recursive Hello:

Callback Hello (a variation on Complementary Recursion):

Leave a Reply

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