Most "calculator" tutorials stop at eval() or at four buttons. A scientific calculator is more interesting: it needs operator precedence, functions that take arguments, a degrees/radians switch, nPr and nCr , and scientific notation like 3.5E8 .
In this post I'll build a small parser that handles all of that in about 120 lines, with no libraries and no eval() . Every example below was run in Node before I wrote it down. The calculator I built it for is here: free online scientific calculator .
Why not just use eval() ?
eval() runs anything you give it, not only maths. If you use it anyway, you must check the input against a strict allow-list first.…