
JavaScript provides 8 mathematical constants that can be accessed as Math properties:
E – the JavaScript E property returns the Euler’s number and the base of natural logarithms (approx. 2.718).
Syntax:
Math.E
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="mathE()">Try it</button>
<p id="example-1"></p>
<script>
function mathE() {
document.getElementById("example-1").innerHTML = Math.E;
}
</script>
</body>
</html>
Output:
Click the button to display Euler’s number.
LN2 – the JavaScript LN2 property returns the natural logarithm of 2 (approx. 0.693).
Syntax:
Math.LN2
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="ln2()">Try it</button>
<p id="example-2"></p>
<script>
function ln2() {
document.getElementById("example-2").innerHTML = Math.LN2;
}
</script>
</body>
</html>
Output:
Click the button to display the natural logarithm of 2.
LN10 – the JavaScript LN10 property returns the natural logarithm of 10 (approx. 2.302).
Syntax:
Math.LN10
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="ln10()">Try it</button>
<p id="example-3"></p>
<script>
function ln10() {
document.getElementById("example-3").innerHTML = Math.LN10;
}
</script>
</body>
</html>
Output:
Click the button to display the natural logarithm of 10.
LOG2E – the JavaScript LOG2E property returns the base-2 logarithm of E (approx. 1.442).
Syntax:
Math.LOG2E;
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="log2e()">Try it</button>
<p id="example-4"></p>
<script>
function log2e() {
document.getElementById("example-4").innerHTML = Math.LOG2E;
}
</script>
</body>
</html>
Output:
Click the button to display the base-2 logarithm of E.
LOG10E – the JavaScript LOG10E property returns the base-10 logarithm of E (approx. 0.434).
Syntax:
Math.LOG10E
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="log10e()">Try it</button>
<p id="example-5"></p>
<script>
function log10e() {
document.getElementById("example-5").innerHTML = Math.LOG10E;
}
</script>
</body>
</html>
Output:
Click the button to display the base-10 logarithm of E.
PI – the JavaScript PI property returns the ratio of a circle’s area to the square of its radius (approx.3.14).
Syntax:
Math.PI
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="pi()">Try it</button>
<p id="example-6"></p>
<script>
function pi() {
document.getElementById("example-6").innerHTML = Math.PI;
}
</script>
</body>
</html>
Output:
Click the button to display PI.
SQRT1_2 – the JavaScript SQRT1_2 property returns the square root of 1/2 (approx. 0.707).
Syntax:
Math.SQRT1_2
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="sqrt()">Try it</button>
<p id="example-7"></p>
<script>
function sqrt() {
document.getElementById("example-7").innerHTML = Math.SQRT1_2;
}
</script>
</body>
</html>
Output:
Click the button to display the square root of 1/2.
SQRT2 – the JavaScript SQRT2 property returns the square root of 2 (approx. 1.414).
Syntax:
Math.SQRT2
Example:
<!DOCTYPE html>
<html>
<body>
<button onclick="sqrt2()">Try it</button>
<p id="example-8"></p>
<script>
function sqrt2() {
document.getElementById("example-8").innerHTML = Math.SQRT2;
}
</script>
</body>
</html>
Output:
Click the button to display the square root of 2.
Enjoy coding!
Read also: