Math in JavaScript is made easier by the language’s support for a series of mathematical constants. If it’s been a while since you’ve dealt with more than multiplication and division, I’ve also included a brief explanation of each in this article.
These JavaScript constants are all properties of the Math
object. Note that, as constants, the properties are UPPERCASE, rather than the camelCase of most JavaScript.
Math.PI
Pi - popularly shortened to 3.14159 - is the circumference - that is, the outside perimeter - of a circle with diameter 1. For circles with a diameter greater or less than 1, we typically use the formula 2 × pi × r (The radius being exactly half the diameter, or the distance between the center of the circle and the perimeter).
Pi is irrational - it goes on forever and ever - and can never be expressed completely as a number, fraction, or decimal. Your browser can’t store an infinite number, so JavaScript shortens PI to 3.141592653589793, which is more than accurate enough for most purposes.
While it’s most common application is finding the circumference and area of circles, pi is also used in probability, statistics, engineering and science: as a universal constant, it shows up everywhere.
Math.SQRT2
The square root of 2. Like pi, it is an irrational number; it is also the length of a diagonal across a square with sides of length 1. JavaScript approximates it to
1.4142135623730951. (Due to rounding errors, Math.SQRT2 * Math.SQRT2
is not exactly 2).
Math.SQRT1_2
The square root of 0.5, equivalent to 1 divided by the square root of 2. Again, an irrational number.
Math.E
e
is also known as Euler’s number, and is the base of natural logarithms. Again, it is an irrational number: JavaScript approximates it as 2.718281828459045
e is the base rate of growth, a constant shared by every continually growing processes; compound interest, population growth, radioactive decay, and more. Examples of these processes are everywhere. Things grow at different rates over time, but they all share a commonality with e
, which can be used as a “growth factor” in calculations. For example, a cell in a human embryo that divides and doubles continuously. In the console:
Math.pow(Math.E,1)
> 2.718281828459045
Math.pow(Math.E,2)
> 7.3890560989306495
Math.pow(Math.E,3)
> 20.085536923187664
Natural Logarithms
The “natural” log is the inverse of e^x
: rather than providing the amount of growth, the natural log (and its relations) provide the time needed to reach a certain level of growth. Math.log(x)
provides the base e
of a given number (x
), but there are several built-in constants:
Math.LN2
The natural logarithm of 2. In JavaScript, rounded to 0.6931471805599453
Math.LN10
The natural logarithm of 10: rounded to 2.302585092994046
Math.LOG2E
The base-2 logarithm of e. Rounded to 1.4426950408889634
Math.LOG10E
The base-10 logarithm of e. Rounded to 0.4342944819032518
Photograph by Petra van der Ree, used under a Creative Commons license
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.