Scientific Calculator
Trigonometry, logarithms, powers and memory, with full keyboard support.
Features
- Trigonometric, inverse trig and hyperbolic functions
- Natural and base-10 logarithms, powers and roots
- Radian and degree modes
- Memory registers and a reusable history
- Safe expression parser, no eval()
How to use it
- Type an expression such as sin(pi/4) * sqrt(2).
- Press Enter or Calculate.
- Switch to degrees if your angles are in degrees.
- Click any history entry to reuse it.
How this evaluates expressions safely
The obvious way to build a calculator in JavaScript is to hand the user's input to eval(). That works, and it is also a security hole: eval executes arbitrary code, so anything typed into the box, or injected into it via a crafted link, runs with the page's full privileges. Plenty of online calculators do exactly this.
This one implements a proper recursive-descent parser instead. The expression is tokenised into numbers, operators, parentheses and function names, then parsed according to precedence rules, parentheses first, then functions and unary minus, then exponentiation, then multiplication and division, then addition and subtraction. Anything the grammar does not recognise is rejected with an error rather than executed.
One detail that surprises people: exponentiation is right-associative, so 2^3^2 means 2^(3^2) = 512, not (2^3)^2 = 64. That matches mathematical convention and most scientific calculators, though some spreadsheet software gets it wrong. Unary minus binds looser than exponentiation too, so -2^2 is β4 rather than 4.
Frequently asked questions
Related tools
Further reading
Read the full guide on the 123MiniApps blog.