JavaScript Calculator
This JavaScript application evaluates mathematical expressions numerically.
It is ideal for simple calculations that would be too tedious with a
hand calculator or
slide rule.
The source code is only a few lines.
No warranty. Use at your own risk. JavaScript
numerics are not perfect, and neither am I.
Special features
In addition to the standard methods and properties of the JavaScript Math object
(summarized below), the following features are supported:
- Prior assignment statements, function definitions, or other
JavaScript statements may be included in the expression window.
As a result, you may write programs for numerical integration
and other special purposes. Terminate all statements
with a semicolon (;). The last expression will be evaluated and
the result displayed.
- Refer to the value displayed in the Result box:
- parsed as a decimal floating point number, R
- parsed as a decimal integer, L
- hyperbolic functions: sinh(), cosh(), tanh()
- inverse hyperbolic functions: asinh(), acosh(), atanh().
Note that acosh() returns positive numbers, but the sign is arbitrary.
- chbase(x,b) - round x to nearest integer and change base to
b, which may range from 2 to 16 (Communicator will take bases up to 36).
The result is a string. Includes correction
for a little-known bug in Navigator 3 which substitutes ':' for 'a' in bases 11
and above.
JavaScript Math object
Expressions are interpreted by JavaScript with the
Math object. Expressions may include the following functions,
which are methods of the JavaScript Math object. Note that the ^
operator does not perform exponentiation - use pow() instead.
- Trigonometry (angles measured in radians):
- sin(x) - sine of x
- cos(x) - cosine of x
- tan(x) - tangent of x
- asin(x) - arc sine of x
- acos(x) - inverse cosine of x
- atan(x) - inverse tangent of x
- Exponentiation/logarithms:
- log(x) - natural logarithm of x
- exp(x) - Euler's constant to the x power
- pow(x,y) - x to the y power
- sqrt(x) - the square root of x
- Miscellaneous:
- abs(x,y) - absolute value of x
- max(x,y) - larger of two numbers
- min(x,y) - smaller of two numbers
- ceil(x) - the smallest integer greater than or equal to x
- floor(x) - the greatest integer less than or equal to x
- round(x) - x rounded to the nearest integer
- random() - a random number between zero and one
In addition, the following constants are defined (as properties of
the Math object):
- E - Euler's constant
- PI - pi
- LN10 - the natural logarithm of 10
- LN2 - the natural logarithm of 2
- SQRT1_2 - the square root of 1/2
- SQRT2 - the square root of 2
If you'd like to learn more, there are helpful JavaScript tutorials at
w3schools.com and
praxent.com
A special shout-out to the Lyndhurst STEM club for girls!
Drop me a line if you have comments or questions:
kankel@physics.montana.edu
This page was last revised 2023 March 1.