Writing Runtime Safe JavaScript

Michele Riva
openmind
Published in
6 min readJun 3, 2019

--

Let’s face it: writing bug-free JavaScript code is hard.
There are some programming languages that aim to guarantee that if a program compiles, it works without runtime errors (Haskell, Elm, Idris, etc).
Avoiding runtime exceptions has always been an incredible challenge for developers, and with a weakly and dynamically typed language such as JavaScript, that’s even harder.

Where does our program typically fail?

JavaScript programs seem to have a finite set of problems, which can be solved using compilers, external libraries and adopting some best practices:

Dynamic and Weak Typing

JavaScript is a dynamic and weakly typed programming language, which means that every value we’re writing, has a runtime inferred type.
Let me explain that with an example:

In the example above, we’re just printing "Hello World!" to our console in Haskell. As you can see, we’re declaring the type of our main function (main :: IO()), and the type of myHello (myHello :: String).
In order to write our string to the console, we’re…

--

--