Diffuse Gedanken eines Dreisterneprogrammierers.

There are only two kinds of languages: the ones people complain about and the ones nobody uses.
— Bjarne Stroustrup

JavaScript is the new C. Similar claims have already been made, but not only do I want to claim that JavaScript is the new C, I also claim that this is not as bad as some people might think.

Now, what most people agree upon, and what might be the reason for the existence of emscripten, is that JavaScript is widespread. Most computers will somehow support it. Is this a coincidence?

It is my observation that a programming language spreads only if it sucks sufficiently, but not too much, and has a single advantage. PHP sucks, but it doesn't matter: It was easy to deploy simple web applications with PHP. C++ sucks, but it is fast, a bit more abstract than and mostly compatible with old C code. Java sucks, but it was the first really practical and portable GUI framework, and the first really widespread web plugin, even before Flash. And of course, we don't have to talk about why C sucks, but its great advantage is its pointer arithmetic and that it can be used as a portable typed assembler.

On the other hand, looking at Haskell, I know many fanboys, but I also talked to people who hate this language or are at least not interested in it. For some people, it sucks too much, while for some people, it fits perfectly — I think this is a general pattern: The broader your audience, the worse your fit for an individual problem. And by evolutionary processes, while the languages that only fit a specific audience will stay inside that audience, the languages that fit all needs somehow will spread, especially if they happen to have one feature that makes them distinct from everything else.

And I think that this is the case with JavaScript. Many programmers appear not to like it much, but it gets most of the jobs done, and it runs everywhere, and it is trivial to deploy - the one feature that makes it distinct from everything else. Yes, C can be compiled for every platform, but deploying applications to people is a lot harder than just telling them to follow your link.

So, JS has many things that make it hard to use. I can totally agree with that. Its possibility to add stuff to objects at will, and just default everything to undefined until something bad happens, is annoying, for example. But so is C: A simple off-by-one-error in an array access can have consequences at a completely different part of the code, that is why there are frameworks to debug and test code. C and C++ use a type system to cope with these problems to some extent. JS does not enforce this, but there is a framework that allows you to do so.

That is pretty much the way it is done in Lisp: You can add type annotations to make the code more efficient and easier to debug, but you do not have to. Not adding type information can be extremely useful in the cases when the actual types are not clear from the beginning ("prototyping") and for small code snippets and simple hacks. Every larger project should utilize types. I would even argue that they should use dependent types, but this is (mostly) tightly coupled with purely functional programming.

By the way, this is not the only parallel to Lisp. And while many Lispers will probably object to JS being a Scheme in C clothes, there is some truth in this: JS has a comparably simple data serialization format (almost as simple as Lisp's), and it has runtime compilation of Strings – which can be used to build a macro system and local domain specific languages. There are even REPLS for JavaScript.

A main difference to Lisp dialects is that it has a richer Syntax that is closer to the commonly used programming languages nowadays, and (except for Common Lisp) it is more complicated to implement, therefore not prone to the Lisp curse.

You can work with it as you would with C/Java and basic structs, but you can as well implement algebraic types. It is probably not particularily good at eiter of these jobs, but also not too bad – it gets the job done. And like Java, it gets the job done without offering too much space for security critical bugs (even though there are things like XSS exploits, of course).

The problem with Java is, that it has the repute of being slow and memory hungry. In my opinion, this was the main disadvantage of all similar languages over C and C++: They are not "on the metal". I would argue that neither is C (and especially C++), but the correspondence to the underlying machine is more direct. JS still has the same problem – even though the JS engines are doing a very good job – but there is a new way people try to address it: Asm.js. It is amazing how fast stuff becomes, and how easy it is to embed it into other JS code. As an example, here is a probabilistic approximation of a Sierpinsky-4-simplex, written mainly in asm.js (notice that you can drag the zero-point and the endpoints of the four axes with the mouse):

On Firefox 36, this still lags a little, but the reason seems to be the rendering engine rather than JS. On Chromium and most WebKit-based browsers, this has no lag at all. Look at the code: it is a portable assembler that runs on every relevant platform. It is compatible with a lot of "legacy code" through emscripten - a killer-feature of C++. And it can be embedded in a programming language with already installed bindings for almost everything, especially, for many things that users get in touch with now. While other languages still bother with libraries for accessing and parsing websites, files, the user's configuration, networking, everything that really matters to most users can be done with JS. There is an API for binary file access, a preliminary API for push notifications, APIs for 2d and 3d graphics, realtime communication.

Of course, I am not saying that C will completely vanish. It did not vanish in favor C++, it did not vanish in favor of Java, and it will not vanish in favor of JavaScript. The Linux Kernel is still written mainly in C, and I do not think that this will change. But the design of shared libraries still bases on C mostly, and the standard library of C is the only thing that is usually taken for granted. My guess is that an increasing number of libraries and other languages will target embeddability in JS rather than C.

Furthermore, I want to make clear that I do not like JS, especially as it is often used superfluously on bad websites, or breaking originally good websites. But remember, we could have ended up with Visual Basic Script. Or CFML. Or XSLT.