ES6 - Math


Advertisements

The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.

Math Properties

Following is a list of all Math properties and its description.

Sr.No Property & Description
1 E

Euler's constant and the base of natural logarithms, approximately 2.718

2 LN2

Natural logarithm of 2, approximately 0.693

3 LN10

Natural logarithm of 10, approximately 2.302

4 LOG2E

Base 2 logarithm of E, approximately 1.442

5 LOG10E

Base 10 logarithm of E, approximately 0.434

6 PI

Ratio of the circumference of a circle to its diameter, approximately 3.14159

7 SQRT1_2

Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707

8 SQRT2

Square root of 2, approximately 1.414

Exponential Functions

The basic exponential function is Math.pow(), and there are convenience functions for square root, cube root, and powers of e, as shown in the following table.

Sr.No Function & Description
1 Math.pow(x, y)

Returns x raised to the power y

2 Math.sqrt(x)

Returns the square root of the number x

3 Math.cbrt(x)

This method returns the cube root of a number x

4 Math.exp(x)

Equivalent to Math.pow(Math.E, x)

5 Math.expm1(x)

Equivalent to Math.exp(x) – 1

6 Math.hypot(x1, x2,...)

Returns the square root of the sum of arguments

Logarithmic Functions

The basic natural logarithm function is Math.log (). In JavaScript, “log” means “natural logarithm.” ES6 introduced Math.log10 for convenience.

Sr.No Function & Description
1 Math.log(x)

Natural logarithm of x

2 Math.log10(x)

Base 10 logarithm of x

3 Math.log2(x)

Base 2 logarithm of x

4 Math.log1p(x)

Natural logarithm of 1 + x

Miscellaneous Algebraic Functions

Following is a list of miscellaneous algebraic functions with their description.

Sr.No Function & Description
1 Math.abs(x)

Absolute value of x

2 Math.sign(x)

The sign of x: if x is negative,–1; if x is positive, 1; and if x is 0, 0

3 Math.ceil(x)

The ceiling of x: the smallest integer greater than or equal to x

4 Math.floor(x)

The floor of x: the largest integer less than or equal to x

5 Math.trunc(x)

The integral part of x (all fractional digits are removed)

6 Math.round(x)

x rounded to the nearest integer

7 Math.min(x1, x2,...)

Returns the minimum argument

8 Math.max((x1, x2,...)

Returns the minimum argument

Trigonometric Functions

All trigonometric functions in the Math library operate on radians, not degrees.

Sr.No Function & Description
1 Math.sin(x)

Sine of x radians

2 Math.cos(x)

Cosine of x radians

3 Math.tan(x)

Tangent of x radians

4 Math.asin(x)

Inverse sine (arcsin) of x (result in radians)

5 Math.acos(x)

Inverse cosine (arccos) of x (result in radians)

6 Math.atan(x)

Inverse tangent (arctan) of x (result in radians)

7 Math.atan2(y, x0)

Counterclockwise angle (in radians) from the x-axis to the point (x, y)

Math.random()

The Math.random() function returns a pseudorandom number between 0 (inclusive) and 1 (exclusive).

Example: Pseudorandom Number Generation (PRNG)

var value1 = Math.random();  
console.log("First Test Value : " + value1 ); 

var value2 = Math.random();  
console.log("Second Test Value : " + value2 ); 

var value3 = Math.random();  
console.log("Third Test Value : " + value3 ); 

var value4 = Math.random(); 
console.log("Fourth Test Value : " + value4 );

Output

First Test Value : 0.5782922627404332 
Second Test Value : 0.5624510529451072 
Third Test Value : 0.9336334094405174 
Fourth Test Value : 0.4002739654388279
Advertisements