javascript math object-mathematical functions examples

It covers the javascript inbuilt mathematical functions library with examples.

Javascript math object

Math is a javascript inbuilt object, and it contains various utility mathematical functions. As every programming language provides mathematical operations as part of programming language.
It contains multiple functions and constants and properties.

Constants and properties

The following are constants declared in Math Object in javascript.

  • Math.E - it returns natural base algorithm - Approximate value is 2.718
  • Math.LN2 - logarithmic of base 2 - 0.693
  • Math.LN10 - logarithmic of base 10 - 2.303
  • Math.LOG2E - Log of E for base 2 - 1.443
  • Math.LOG10E - Log of E for base 10 - 0.434
  • Math.PI - Outputs PI value - 3.14
  • Math.SQRT1_2 - Square root of half of the number
  • Math.SQRT2 - Square root of two value

Static Inbuilt Functions in Math Library

It provides various mathematical inbuilt static functions.

Math.abs() - Absolute value function

This function returns the absolute value of a given number.

The parameter is n,

if the number is greater than zero, returns the same number

if the number is zero, returns zero

if the number is less than zero, return a positive number

Math.abs(5.3); //5.3
Math.abs(-5.3); //5.3
Math.abs(null); //0
Math.abs("test"); //NaN

Math.acos() - arccosine value function

This function returns the arccosine of a value. Parameter number takes a value between -1 and 1

console.log(Math.acos(0.1)); //1.4706289056333368
console.log(Math.acos(5)); // NaN
console.log(Math.acos(null)); // 1.5707963267948966

Math.acosh() - hyperbolic arc-cosine value function

This method performs hyperbolic arc-cosine and returns the value.

console.log(Math.acosh(0.1)); //NaN
console.log(Math.acosh(5)); // 2.2924316695611777
console.log(Math.acosh(null)); // NaN

Math.asin(x) Math.asinh(x) functions

  • Math.asin(x) - This method performsn arcsine of a value
  • Math.asinh(x) - This method performs hyperbolic arcsine of a value
console.log(Math.asin(0.1)); //0.1001674211615598
console.log(Math.asin(5)); // Nan
console.log(Math.asin(null)); // 0

console.log(Math.asinh(0.1)); //0.09983407889920758
console.log(Math.asinh(5)); //2.3124383412727525
console.log(Math.asinh(null)); // 0

arctangent functions

  • Math.atan(x) - Performsn mathmatical arctangent of avalue
  • Math.atanh(x) - Performsn mathmatical hyperbolic arctangent of avalue Math.atan2(y, x) - hyperbolic quotient of a value
console.log(Math.atan(0.1)); //0.09966865249116204
console.log(Math.atan(5)); //1.373400766945016
console.log(Math.atan(null)); // 0

console.log(Math.atanh(12)); //NaN
console.log(Math.atanh(0.1)); //0.10033534773107558
console.log(Math.atanh(null)); // 0

console.log(Math.atan2(12, 3)); //1.3258176636680326
console.log(Math.atan2(10, 4)); //1.1902899496825317
console.log(Math.atan2(null, null)); // 0

Math.cbrt() - cube root calculation

This function performs the cube root of a given number.

console.log(Math.cbrt(8)); //2
console.log(Math.cbrt(10)); //2.154434690031884
console.log(Math.cbrt(null)); // 0

Math.ceil() - Rounds the upper number

This method returns the rounded upper number.

Math.ceil(0.75); //  1
Math.ceil(0.45); //  1
Math.ceil(2); //  2
Math.ceil(4.3); //  4
Math.ceil(-3.2); // -3
Math.ceil(-3.9); // 3

Math.floor() - Rounds lowest number

This method returns the rounded lowest number.

Math.floor(0.75); //  0
Math.floor(0.45); //  0
Math.floor(2); //  2
Math.floor(4.3); //  4
Math.floor(-3.2); // -3
Math.floor(-3.9); // -4

Math.random()- Random Number Generator

This function generates a random number between 0 and 1

console.log(Math.random()); //0.044142306688350175
console.log(Math.random()); //0.5684720489854178

Math.max() - Find Maximum number

It returns the maximum number from zero or more numbers.

console.log(Math.max(4, 51)); //51
console.log(Math.max(1, 5)); //5
console.log(Math.max(0, null, 3)); //3

Math.min() - Find Miniumum number

console.log(Math.min(4, 51)); //4
console.log(Math.min(1, 5)); //1
console.log(Math.min(0, null, 3)); //0

Math.pow() - exponent power base

This function performs the exponent power base of a number.

console.log(Math.pow(2, 3)); //8
console.log(Math.pow(5, 3)); //125

Math.round() - Round number

This method returns the rounded nearest number.

console.log(Math.round(3.1)); //3
console.log(Math.round(-3.1)); //-3
console.log(Math.round(3.9)); //4
console.log(Math.round(-3.9)); //-4

Math.trunc() - truncate number

This method returns the truncate number, It omits decimal values.

console.log(Math.trunc(3.1)); //3
console.log(Math.trunc(-3.1)); //-3
console.log(Math.trunc(3.9)); //3
console.log(Math.trunc(-3.9)); //-3

Math.sqrt() - Square root of a number

This method returns the square root of a number.

console.log(Math.sqrt(16)); //4
console.log(Math.sqrt(25)); //5
console.log(Math.sqrt(100)); //10
console.log(Math.sqrt(23)); //4.795831523312719

Math.tan(), Math.tanh() - tangent number

Math.tan() - tangent of a given value
Math.tanh() - hyperbolic tangent of a given argument

console.log(Math.tan(16)); //0.3006322420239034
console.log(Math.tan(25)); //-0.13352640702153587
console.log(Math.tanh(100)); //1
console.log(Math.tanh(23)); //1

Math.sign(), Math.sin() - sign number

Math.sin() - sign of a given argument
Math.sign() - Sign of a given positive,negative or zero number

console.log(Math.sign(16)); //1
console.log(Math.sign(-10)); //-1
console.log(Math.sign(0)); //0
console.log(Math.sin(16)); //-0.2879033166650653
console.log(Math.sin(-10)); //0.5440211108893698
console.log(Math.sin(0)); //0