JavaScript Reference ยท Math
Math.PI, Math.E and the other Math constants
Reference for the eight Math constants: PI, E, LN2, LN10, LOG2E, LOG10E, SQRT2 and SQRT1_2, with their values, uses and read-only behavior.
The Math object holds eight numeric constants. The most famous is Math.PI, the ratio of a circle's circumference to its diameter. Math.E is Euler's number, the base of natural logarithms. The other six are handy square-root and logarithm values that you would otherwise have to compute yourself.
All of them are properties, not functions, so you write Math.PI with no parentheses. They are read-only: assigning to them does nothing in sloppy mode and throws a TypeError in strict mode.
Math.PI Math.E
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Math.PI // 3.141592653589793 Math.E // 2.718281828459045 Math.LN2 // 0.6931471805599453 Math.LN10 // 2.302585092994046 Math.LOG2E // 1.4426950408889634 Math.LOG10E // 0.4342944819032518 Math.SQRT2 // 1.4142135623730951 Math.SQRT1_2 // 0.7071067811865476
| Constant | Meaning |
|---|---|
Math.PI |
The ratio of a circle's circumference to its diameter. |
Math.E |
Euler's number, the base of the natural logarithm. |
Math.LN2 |
The natural logarithm of 2. |
Math.LN10 |
The natural logarithm of 10. |
Math.LOG2E |
The base-2 logarithm of e, that is 1 / Math.LN2. |
Math.LOG10E |
The base-10 logarithm of e, that is 1 / Math.LN10. |
Math.SQRT2 |
The square root of 2. |
Math.SQRT1_2 |
The square root of 1/2, which equals 1 / Math.SQRT2 up to rounding. |
Exceptions: none on reading. Writing in strict mode throws a TypeError.
Usage notes
- Approximations. Every value is the nearest double to the true constant, so about 15 to 17 significant digits. Results of trigonometric formulas show tiny errors, such as
Math.sin(Math.PI)not being exactly0. - Don't hard-code 3.14.
Math.PIis far more accurate and documents your intent. - Tau. There is no
Math.TAU; defineconst TAU = 2 * Math.PIif full turns make your code clearer. - Degrees and radians. Convert with
degrees * Math.PI / 180. - Logarithm shortcuts.
Math.log(x) / Math.LN10works as a base-10 logarithm, butMath.log10()is more accurate. - Related numeric constants live on
Number, notMath:Number.EPSILON,Number.MAX_VALUE,Number.MAX_SAFE_INTEGER. - Case matters.
Math.piisundefined, and using it givesNaNin arithmetic.
Browser support
Supported in every browser and in Node.js.