A while back I wrote a solution to a kata (challenge) on Codewars which asked for a function named greet() which would return the string “hello world!”. I had fun writing it, and thought the concept I came up with was interesting, so I shared it on Reddit too.

Now, based on the feedback received, I am writing this article to explain how it works.

And so, without further ado, let me present, “Hello world using prime numbers”.

Go ahead and take a minute to copy that into your browser console, define the greet() function, and then execute it.

But how does this snippet work and why did I write it this way?

Why?

Let me talk about the why first. Badly written code is not always the result of attempts by novice or inept programmers. Ever since code has been written there have been reasons to intentionally write it in a way which renders it unintelligible to the casual glance.

Sometimes it may be done to work around a problem, or as a quick fix. We as programmers know this affectionately as a kludge. While nobody wants to be the author of a kludge, often the time or budget available leaves no other choice.

Another reason one may end up intentionally creating difficult to read code is for performance reasons. Manual optimisation may result in taking advantage of quirks within the architecture or even dropping down to a lower level like assembly language. Automated tools, such as JavaScript minifiers, would also fall into this category.

This code was written for neither of those reasons, but rather as an exercise in obfuscation. While obfuscation may be used to hide the meaning of code for security purposes, this security through obscurity is not recommended.

There is however a long and distinguished history of writing obfuscated code as a pastime. This is yet another addition to that legacy.

How?

In order to understand how this code works there are a number of tricks and obscure language rules that need to be understood. To get started, here is a version of the code which has been cleaned up a bit by using multiple lines and adding some spacing to make it more readable.

Note that this code will not function correctly anymore due the function’s self-referential nature. But more on that later.

The function starts with a return consisting of a list of statements separated by commas. When such a list is processed in JavaScript, the result of the last statement in the list will be the value that is returned. This means that the earlier statements will not be returned directly, but will have the side-effect of initialising the required variables. Here is a simple example.

Variables m, r, x and y are set to the numbers which are used to drive the function. The variable z is set to 3 to the power of 3 for a final value of 27.

Setting variable l is a trick which comes from another obscure language feature. If two arrays are added together the result is coerced to a string. Since the arrays are empty, l ends up containing an empty string.

A similar trick is used to set variable g. The function itself is coerced to a string by adding it to the empty string l. This means that g contains a copy of the entire source of the function.

The formula on the next line looks simple enough but is written to be a red herring. Again the empty string l makes an appearance. In this case it causes the formulae on either side to be calculated mathematically and the results of each to be concatenated together as a string. The following example illustrates this point.

This long string of digits is then split up into an array of 2 digit numbers by the call to match() which is in turn sent to map() which performs further arithmetic to find a set of positions within an array.

When a second parameter is used with the map() function, “this” is set to a the specified value within the callback function. Here it is set to a string and individual characters within the string are referenced.

The string is created by manipulating the contents of variable g, which contains the source code of the function itself. The self-reference used here forms a large part of the “magic” by giving the function raw data to work with.

To start with the data is run through replace(). Here one finds more code designed to cause confusion at first glance. The regular expression removes any character which is not between ^ and {, is not a space and is not an exclamation mark. The trick here is that the range of characters between ^ and { contains all lowercase letters.

After that split(“”) is called to change the string into an array of characters. The array of characters is sorted using sort(), a filter() callback is used to remove any duplicates, and then join(“”) is used to turn the result back into a string.

Finally the numbers calculated within the map() are matched up with positions in the string in the callback return. The resulting array is passed through join(“”) again to turn it into the string “hello world!”.

Some of you may still be wondering how the prime numbers were originally calculated. I will not go into detail because I think it is worth leaving a little bit of mystery.

Suffice to say that when creating something like this one must work backwards, starting with the original, simple code and adding levels of obfuscation.

If you enjoyed this, you may also want to read my article “Writing highly readable code”. This has more information on how to avoid ending up with code that is difficult to read.