JavaScript exposes common mathematical constants as read-only properties of the built-in Math object. The most frequently used is Math.PI.
const radius = 2;
const circumference = 2 * Math.PI * radius;
console.log(circumference); // 12.566370614359172| Constant | Meaning |
|---|---|
Math.PI | Ratio of a circle's circumference to its diameter. |
Math.E | Euler's number, the base of natural logarithms. |
Math.LN2 | Natural logarithm of 2. |
Math.LN10 | Natural logarithm of 10. |
Math.LOG2E | Base-2 logarithm of e. |
Math.LOG10E | Base-10 logarithm of e. |
Math.SQRT2 | Square root of 2. |
Math.SQRT1_2 | Square root of 1/2. |
const radius = 5;
const area = Math.PI * radius ** 2;
console.log(area);console.log(Math.E);
console.log(Math.exp(1)); // same mathematical value as e, subject to Number precisionMath.LN2 and Math.LN10 are useful when natural logarithms are involved. For direct logarithm calculations, see Math.log().
console.log(Math.LN2);
console.log(Math.log(2));Math.SQRT2 and Math.SQRT1_2 provide commonly used square-root values. For arbitrary inputs, use Math.sqrt().
The displayed decimal values are approximations stored as JavaScript Number values. Avoid comparing long decimal results by typing a rounded decimal literal when a tolerance comparison is more appropriate.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.