Brainfuck interpreter in JavaScript

“Brainfuck? Something wrong?”

Last week I somehow managed to come across brainfuck, the esoteric programming language,  again.  I had read about it some years ago and also had the idea once to make a brainfuck interpreter.


Why? Do we always need a rational reason? Maybe I just wanted to see if it will fuck up my brain or if I will stay mentally sane. Maybe I just thought it’s very funny. Maybe god decided I had to try to do it… nah… not in my reality… anyway…


Well… Last week I came across it again. Because I’m working much with PHP and Javascript these days I thought I could give it another try. Which language to pick? Well… PHP is a server side language. You need some sort of input for covering all commands of brainfuck. You could handle that with some Ajax-Requests. But what if something goes wrong (like an infinite loop?) . Well… PHP scripts get killed after 60 seconds or whatever the value in the ini-file or the override-setting in the source file is set to.  What about debugging? You could also handle that with Ajax. Anyway I didn’t even think about those questions until now. I just wanted to create a brainfuck interpreter and picked the language Javascript.

“So what is brainfuck?”

Brainfuck is a so called ‘esoteric programming language’ created by a Swiss guy named Urban Müller in 1993 (also check the wikipedia article).  A esoteric programming language is a fun thing. An instrument to practice logical thinking if you want. There’s even a ‘dialect’ of brainfuck for Orang-Utans called Ook!. Now that’s silly!

Brainfuck consists of 8 commands

  • You have two commands to move the pointer in memory ( > ,  < ) .
  • You have two commands to changed the values in the memory ( + , – ).
  • You have two commands to open/close loops ( [ , ] ). If memory[pointer] == 0 the loop will end. So you have to do some sort of loop-counter, else you will get an infinite loop.
  • You have two commands for output (.) and input (,) . The  dot “.”  will output the character based on the ASCII-value that is in the current memory location. The comma “,” will get a character from the user. The ASCII-value will be saved to the current memory location.

So …  you move around in memory and increase, decrease and display values. You can actually do very complex calculations like multiplication, division, etc. You just have to think how you can achieve that in brainfuck.




++ Increase value of first memory cell two times = 2

[>++<-] loop until first memory cell is 0 , in every loop move to the second cell, add 2 to the second cell and then go back to the first cell and decrease its value with 1

In the end the first cell will be empty and the second cell will contain the value 4.


We take example 1) and add some more code.


++ Increase value of first cell two times = 2

[>++ Move to second cell and increase value two times = 2

[>++<-] Move to third cell, increase value by 2, move back to second cell and decrease value by 1

<-] Move to first cell and decrease value by one.

These nested loops will increase the value of the third cell to 8. The other two cells will contain the value 0 because they were ‘used’ by the loops.

“Very interesting … bla bla bla …  and now?”

Now you can try this no my Javascript brainfuck interpreter here. The example program in the textarea displays a text. Just klick on the button “parse”.

If you find errors please tell me.

Happy coding. 😉


3 thoughts on “Brainfuck interpreter in JavaScript

    1. A compiler that generates Brainfuck code from something else or that compiles Brainfuck code to executable code? hmmm… gotta think about that.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s