Tutorials Tonight's Logo

JAVASCRIPT MATH


In javascript, Math is a built-in object which has many properties and methods that is used to deal with mathematical constants and functions.

Math is not a constructor, Unlike Array or String. All the methods and properties of Math are static, meaning you can directly use methods and properties with class names. example Math.PI, Math.sin(x) etc.

Note: Static methods and properties belong to class not to its object. All the methods and properties can be called on Math as an object.

var pi = Math.PI; // 3.141592653589793
console.log(pi);
var sine = Math.sin(0.5); // 0.479425538604203
console.log(sine);
▶ Run the code

Math Object Properties

Property Description
Math.PI returns value of pi (ratio of circumference to diameter)
Math.E returns euler's constant
Math.LN2 returns natural logarithm of 2 (approx 0.693)
Math.LN10 returns natural logarithm of 10 (approx 2.303)
Math.LOG2E returns logarithm of E at base 2 (approx 1.443)
Math.LOG10E returns logarithm of E at base 10 (approx 0.434)
Math.SQRT1_2 returns Square root of 1/2 (approx 0.707)
Math.SQRT2 returns Square root of 2 (approx 1.414)

Math.PI

var pi = Math.PI; // 3.141592653589793
console.log(pi);
▶ Run the code

Math.E

var eular = Math.E; // 2.718281828459045
console.log(eular);
▶ Run the code

Math.LN2

var ln2 = Math.LN2; // 0.6931471805599453
console.log(ln2);
▶ Run the code

Math.LN10

var ln10 = Math.LN10; // 2.302585092994046
console.log(ln10);
▶ Run the code

Math.LOG2E

var log2e = Math.LOG2E; // 1.4426950408889634
console.log(log2e);
▶ Run the code

Math.LOG10E

var log10e = Math.LOG10E; // 0.4342944819032518
console.log(log10e);
▶ Run the code

Math.SQRT1_2

var sqroot = Math.SQRT1_2; // 0.7071067811865476
console.log(sqroot);
▶ Run the code

Math.SQRT2

var sqroot = Math.SQRT2; // 1.4142135623730951
console.log(sqroot);
▶ Run the code


Javascript Math methods

Method Description
Math.abs(x) Returns the absolute value of x
Math.acos() Returns the arccosine of given number in radian
Math.acosh() Returns the hyperbolic arccosine of a number
Math.asine() Returns the arcsine of given number in radian
Math.asinh() Returns the hyperbolic arccosine of a number
Math.atan() Returns the arctangent of given number in radian
Math.atan2(y,x) Returns the arctangent of quotient of its argument
Math.atanh(y,x) Returns the hyperbolic arctangent of x
Math.cbrt(x) Returns the cube root of x
Math.ceil(x) Returns the smallest integer greater than or equal to x
Math.cos(x) Returns the cosine of x
Math.cosh(x) Returns the hyperbolic cosine of x
Math.exp(x) Returns the Ex, where E is Euler number
Math.floor(x) Returns the largest integer less than or equal to x
Math.log(x) Returns the natural logarithm of x (loge )
Math.log10(x) Returns the base-10 logarithm of x
Math.log2(x) Returns the base-2 logarithm of x
Math.pow(x,y) Returns base x to exponent y ( xy )
Math.random() Returns a random number between 0 and 1
Math.round(x) Returns the value of number rounded to nearest integer
Math.sign(x) Returns sign of x, +ve -ve or 0
Math.sqrt(x) Returns the square root of x

Lets see few Examples.

The abs() method

The Math.abs(x) method return absolute value of x.

console.log(Math.abs(-5)); // 5
console.log(Math.abs(0)); // 0
console.log(Math.abs(-2.5)); // 2.5
console.log(Math.abs({})); // NaN
console.log(Math.abs(null)); // 0
console.log(Math.abs(undefined)); // NaN
console.log(Math.abs(Infinity)); // Infinity
console.log(Math.abs([12])); // 12
console.log(Math.abs([2,4])); // NaN
▶ Run the code

The Math.floor(x) and Math.ceil(x) method

The Math.floor(x) method return largest number less than or equal to x.

The Math.ceil(x) method return smallest number greater than or equal to x.

console.log(Math.floor(8.5243));
console.log(Math.floor(-34.67));
console.log(Math.floor(5.85));
console.log(Math.floor(Math.PI));

console.log(Math.ceil(8.5243));
console.log(Math.ceil(-34.67));
console.log(Math.ceil(5.85));
console.log(Math.ceil(Math.PI));
▶ Run the code

The random() method

The Math.random() method returns a random number in between 0 and 1.

This method can be used to get a random number between any integer.

console.log(Math.random()); // random number between 0 - 1
console.log(Math.random()*10); // random number between 0 - 10
console.log(Math.floor(Math.random() * 15)); // Integer random number between 0 - 15
▶ Run the code

The round(x) method

The Math.round(x) method returns the value of the round number nearest to x.

console.log(Math.round(8.5243));
console.log(Math.round(-8.5));
console.log(Math.round(2.325));
console.log(Math.round(-2.5));
▶ Run the code

Trigonometric operation in Javascript

The Math object has many built-in trigonometric methods that can be used to calculate Trigonometric values.

console.log(Math.sin(15));
console.log(Math.sinh(15));
console.log(Math.cos(15));
console.log(Math.cosh(15));
console.log(Math.tan(15));
console.log(Math.tanh(15));
▶ Run the code

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.