Categories
JavaScript Web development

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks.

All JavaScript methods/properties of Math can be called by using Math as an object (without creating it).

Example:

<!DOCTYPE html>
<html>
<body>

<p><b>Math.PI returns PI:</b></p>
<p id="piExample"></p>

<p><b>Math.sqrt(16) returns the square root of 16:</b></p>
<p id="sqrtExample"></p>

<script>
var x = Math.PI;
var y = Math.sqrt(10);

document.getElementById("piExample").innerHTML = x;
document.getElementById("sqrtExample").innerHTML = y;
</script>

</body>
</html>

Output:

Math.PI returns PI:

Math.sqrt(16) returns the square root of 16:


Math object properties:

E – the JavaScript E property returns the Euler’s number and the base of natural logarithms (approx. 2.718).

LN2 – the JavaScript LN2 property returns the natural logarithm of 2 (approx. 0.693).

LN10 – the JavaScript LN10 property returns the natural logarithm of 10 (approx. 2.302).

LOG2E – the JavaScript LOG2E property returns the base-2 logarithm of E (approx. 1.442).

LOG10E – the JavaScript LOG10E property returns the base-10 logarithm of E (approx. 0.434).

PI – the JavaScript PI property returns the ratio of a circle’s area to the square of its radius (approx.3.14).

SQRT1_2 – the JavaScript SQRT1_2 property returns the square root of 1/2 (approx. 0.707).

SQRT2 – the JavaScript SQRT2 property returns the square root of 2 (approx. 1.414).

Math Object Methods:

abs(x) – the JavaScript abs() method returns the absolute value of a number.

acos(x) – the JavaScript Math.acos() returns the arccosine of a number as a value between 0 and PI radians.

acosh(x) – the JavaScript Math.acosh() returns the hyperbolic arccosine of a number.

asin(x) – the JavaScript Math.asin() returns the arcsine of a number as a value between -PI/2 and PI/2 radians.

asinh(x) – the JavaScript Math.asinh() returns the hyperbolic arcsine of a number.

atan(x) – the JavaScript Math.atan() returns the arctangent of a number as a value between -PI/2 and PI/2 radians.

atan2(y,x) – the JavaScript Math.atan2() returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians.

atanh(x) – the JavaScript Math.atanh() returns the hyperbolic arctangent of a number.

cbrt(x) – the JavaScript Math.cbrt() returns the cubic root of a number.

ceil(x) – the JavaScript Math.ceil() rounds a number upwards to the nearest integer, and returns the result.

clz32(x) – the JavaScript Math.clz32() returns the number of leading zeros in a 32-bit binary representation of a number.

cos(x) – the JavaScript Math.cos() returns the cosine of a number.

cosh(x) – the JavaScript Math.cosh() returns the hyperbolic cosine of a number.

exp(x) – the JavaScript Math.exp() returns the value of Ex, where E is Euler’s number (approx. 2.7183) and x is the number passed to it.

expm1(x) – The JavaScript Math.expm1() returns the value of Ex minus 1, where E is Euler’s number (approx. 2.7183) and x is the number passed to it.

floor(x) – the JavaScript Math.floor() method rounds a number (downwards) to the nearest integer and returns the result.

fround(x) – the JavaScript Math.fround() returns the nearest (32-bit single precision) float representation of a number.

log(x) – the JavaScript Math.log() returns the natural logarithm (base E) of a number.

log10(x) – the JavaScript Math.log10() returns the base-10 logarithm of a number.

log1p(x) – the JavaScript Math.log1p() returns the natural logarithm (base E) of 1 + a defined number.

log2(x) – the JavaScript Math.log2() returns the base-2 logarithm of a number.

max(x,y,z…) – the JavaScript Math.max() returns the number with the highest value.

min(x,y,z…) – the JavaScript Math.min() method returns the number with the lowest value.

pow(x,y) – the JavaScript Math.pow() returns the value of x to the power of y (xʸ).

random(x) – the JavaScript Math.random() returns a random number from 0 (included) up to but not including 1.

round(x) – the JavaScript Math.round() rounds a number to the nearest integer.

sign(x) – the JavaScript Math.sign() returns whether a number is negative, positive or zero.

sin(x) – the JavaScript Math.sin() returns the sine of a number.

sqrt(x) – the JavaScript Math.sqrt() returns the square root of a number.

tan(x) – the JavaScript Math.tan() returns the tangent of a number.

tanh(x) – the JavaScript Math.tanh() returns the hyperbolic tangent of a number.

trunc(x) – the JavaScript Math.trunc() returns the integer part of a number.

Read also:

JavaScript Introduction

JavaScript HTML DOM

Categories
JavaScript Web development

JavaScript Math.floor() Method

JavaScript Math.floor() Method

The JavaScript Math.floor() method rounds a number (downwards) to the nearest integer and returns the result.

Syntax:

Math.floor(x)

Example1:

Click the button to round the number 2.7 downward to its nearest integer:

<!DOCTYPE html>
<html>
<body>

<button onclick="floorMethod()">Try it</button>
<p id="example-1"></p>

<script>
function floorMethod() {
  document.getElementById("example-1").innerHTML = Math.floor(2.7);
}
</script>

</body>
</html>

Output:


Example2:

Use the JavaScript Math.floor() method on different numbers:

<!DOCTYPE html>
<html>
<body>

<button onclick="floorMethod2()">Try it</button>
<p id="example-2"></p>

<script>
function floorMethod2() {
  var a = Math.floor(0.80);
  var b = Math.floor(0.95);
  var c = Math.floor(4.2);
  var d = Math.floor(6.1);
  var e = Math.floor(-8.9);
  var f = Math.floor(-3.3);

  var x = a + "," + b + "," + c + "," + d + "," + e + "," + f;
  document.getElementById("example-2").innerHTML = x;
}
</script>

</body>
</html>

Output:


Enjoy coding!