The day I tried to recreate life in JavaScript

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Here's an experiment I did a few years ago, a self-generating JavaScript program!

Article published on 25/06/2020, last updated on 09/08/2026

I've always been passionate about science in general, but there's one concept in particular that fascinates me, both in nature and in computing: randomness.

Of course, the more pedantic among you will tell me that true randomness doesn't exist in computing (that's true), and others will ask me why?

My answer: maybe in a future article!

Anyway, many of my experiments revolve around this concept, and today's is of course no exception.

My idea: try to draw inspiration from the concept of evolution theorized by Charles Darwin to generate a program and make it evolve randomly, as if it were alive itself.

The context

To make a program evolve, like a living being, you need several things:

  • an environment, here it's the JavaScript execution engine
  • elementary building blocks (DNA), here these are alphanumeric characters and a few special characters
  • a notion of time and survival, here we'll use the "exec" function to run the generated program. If the code executes without returning an error, it will be considered a survivor.
  • a genetic memory, each surviving version of the program will be used for the next evolution, otherwise we'll revert to the previous one.

Even though this may sound complicated, you'll see that the result is actually very simple!

The program

I carried out this experiment a few years ago; the program is written in JavaScript, and the code isn't the cleanest, but it does exist.

Here's the generator's source code, which you can also find on my Github: https://github.com/NicolasBrondin/living-js

The result

With each new surviving version, the program tries to grow its genetic code. Very often without success. But here are some results of "living" programs, which are quite whimsical for us humans to read...

And they all run perfectly!

Three interesting things emerge from this experiment:

  • The smallest valid JavaScript program will be a digit between 0 and 9, or a semicolon.
  • The longest programs, and the ones generated the fastest, are comments, because the combination "//" makes the rest of the program valid.
  • The majority of generated programs are actually values

Evolution

Since the program is really very simple, it would be interesting to add new features such as:

  • Using keywords as primary building blocks, and perhaps even following the language's syntax for more interesting programs
  • Adding a more specific heuristic than simple execution, such as execution time, memory used, and so on
  • Improving the evolution method to allow the program to change pieces within the program, and not just at the end

Danielle MacInnes sur Unsplash

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet