Categories
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
Web development

JavaScript Math.trunc()

JavaScript Math.trunc()

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

Syntax:

Math.trunc(number)

Note: The JavaScript Math.trunc() won’t round the number up/down to the nearest integer, but simply remove the decimals.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
document.getElementById("example").innerHTML = Math.trunc(7.57);
</script>

</body>
</html>

Output:

The JavaScript Math.trunc() returns the integer part of a number:


Note: The JavaScript Math.trunc() is not supported in Internet Explorer.

Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math Object Properties

JavaScript Math.round()

Categories
Web development

JavaScript tan() & tanh() Methods

JavaScript tan() & tanh() Methods

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

Syntax:

Math.tan(number)

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
document.getElementById("example").innerHTML = Math.tan(1);
</script>

</body>
</html>

Output:

The JavaScript Math.tan() returns the tangent of a number:


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

Syntax:

Math.tanh(number)

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example-1"></p>

<script>
document.getElementById("example-1").innerHTML = Math.tanh(1);
</script>

</body>
</html>

Output:

The JavaScript Math.tanh() returns the hyperbolic tangent of a number:


Note: The JavaScript Math.tanh() is not supported in Internet Explorer.

Enjoy coding!

Read also:

JavaScript Math Object

JavaScript atan(), atan2() & atanh() Methods

JavaScript cos() & cosh() Methods

Categories
Web development

JavaScript Math.sqrt()

JavaScript Math.sqrt()

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

Syntax:

Math.sqrt(number)

Note: If a number is negative , NaN is returned.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.sqrt(0);
let b = Math.sqrt(1);
let c = Math.sqrt(7);
let d = Math.sqrt(45);
let e = Math.sqrt(-9);

document.getElementById("example").innerHTML =
a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e;
</script>

</body>
</html>

Output:

The JavaScript Math.sqrt() returns the square root of a number:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.cbrt()

JavaScript Math.clz32()

Categories
Web development

JavaScript Math.sin()

JavaScript Math.sin()

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

Syntax:

Math.sin(number)

Note: JavaScript Math.sin(x) returns a value between -1 and 1, which represents the sine of the parameter x, or NaN if the value is empty.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.sin(2);
let b = Math.sin(-2);
let c = Math.sin(0);
let d = Math.sin(Math.PI);
let e = Math.sin(Math.PI / 2);
let f = Math.sin();

document.getElementById("example").innerHTML =
a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
</script>

</body>
</html>

Output:

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


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.ceil()

JavaScript Math.pow()

Categories
Web development

JavaScript Math.sign()

JavaScript Math.sign()

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

Syntax:

Math.sign(number)

Note:

If the number is positive, this method returns 1.
If the number is negative, it returns -1.
If the number is zero, it returns 0.
If the number is not a number, it returns NaN.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.sign(2);
let b = Math.sign(-2);
let c = Math.sign(0);
let d = Math.sign();
  
document.getElementById("example").innerHTML =
a + "<br>" + b + "<br>" + c + "<br>" + d;
</script>

</body>
</html>

Output:

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


Note: JavaScript Math.sign() is not supported in Internet Explorer.

Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.floor() Method

JavaScript Math Object Properties

Categories
Web development

JavaScript Math.round()

JavaScript Math.round()

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

Syntax:

Math.round(number)

Note: 2.49 will be rounded to 2, and 2.5 will be rounded to 3.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.round(2.77);
let b = Math.round(2.43);
let c = Math.round(2.50);
let d = Math.round(-2.65);
let e = Math.round(-2.23);
let f = Math.round(-2.49);

document.getElementById("example").innerHTML = 
a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
</script>

</body>
</html>

Output:

Math.round() rounds a number to the nearest integer:

Enjoy coding!

Read also:

JavaScript Math.fround()

JavaScript random() Method

JavaScript Math Object

Categories
Web development

JavaScript Math.pow()

JavaScript Math.pow()

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

Syntax:

Math.pow(x, y)

x – the base.

y – the exponent.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.pow(0, 2);
let b = Math.pow(1, 1);
let c = Math.pow(1, 7);
let d = Math.pow(3, 4);
let e = Math.pow(-3, 3);
let f = Math.pow(1, 4);

document.getElementById("example").innerHTML = 
a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
</script>

</body>
</html>

Output:

Math.pow(x,y) returns the value of x to the power of y:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math Object Properties

JavaScript Math.log()

Categories
Web development

JavaScript max() & min() Methods

JavaScript max() & min() Methods

The JavaScript Math.max() returns the number with the highest value.

Syntax:

Math.max(n1, n2, n3, ..., nX)

n1, n2, n3, …, nX (optional ) – one or more numbers to compare.

Note: A Number, representing the highest number of the arguments, or -Infinity if no arguments are given, or NaN if one or more arguments are not numbers.

Example:

<!DOCTYPE html>
<html>
<body>

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

<script>
function maxFunction() {
  document.getElementById("example-1").innerHTML = Math.max(3, 7);
}
</script>

</body>
</html>

Output:

Click the button to return the highest number of 3 and 7:


The JavaScript Math.min() method returns the number with the lowest value.

Syntax:

Math.min(n1, n2, n3, ..., nX)

n1, n2, n3, …, nX (optional ) – one or more numbers to compare.

Example:

<!DOCTYPE html>
<html>
<body>

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

<script>
function minFunction() {
  document.getElementById("example-2").innerHTML = Math.min(3, 7);
}
</script>

</body>
</html>

Output:

Click the button to return the lowest number of 3 and 7:


Read also:

JavaScript Math Object

JavaScript atan(), atan2() & atanh() Methods

JavaScript Math.fround()

Categories
Web development

JavaScript Math.log2()

JavaScript Math.log2()

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

Syntax:

Math.log2(number)

Note: A Number, representing the base-2 logarithm of a number.
If the number is -1, -Infinity is returned.
If the number is less than -1, it returns NaN

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="log2Function()">Try it</button>
<p id="example"></p>

<script>
function log2Function() {
  var a = Math.log2(2.225);
  var b = Math.log2(2);
  var c = Math.log2(1);
  var d = Math.log2(0);
  var e = Math.log2(-1);

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

</body>
</html>

Output:

Return the base 2 logarithm of different numbers:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.log()

JavaScript Math.log10()