Calculators

It seems to be something of a Javascript beginners’ hobby to try to write a calculator — questions about how one would go about doing that are constantly popping up in coding forums all over the place. There are already hundreds of examples out there, of course, but it seems the stress of an imminent homework deadline incapacitates the searching ability of most students.

In this article, I will try to outline a few different approaches you can take when confronted with this rather fun task:

First, a very simple calculator that leverages Javascript’s ability to evaluate strings as code.

Second, an OOP approach that gives you a great deal of flexibility, while trying not to overengineer things.

Third, based on the OOP approach, an example of how to overcome the size limit of Javascript numbers, in order to be able to work with very large numbers (without losing precision).