Saturday, November 17, 2018

JavaScript Math

The JavaScript Math object allows you to perform mathematical tasks on numbers.

Math.PI;            // returns 3.141592653589793

Math.round()


Math.round(x) returns the value of x rounded to its nearest integer:

Math.round(4.7);    // returns 5Math.round(4.4);    // returns 4

Math.pow()

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

Math.pow(82);      // returns 64

Math.sqrt()

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

Math.sqrt(64);      // returns 8

Math.abs()

Math.abs(x) returns the absolute (positive) value of x:

Math.abs(-4.7);     // returns 4.7

Math.ceil()

Math.ceil(x) returns the value of x rounded up to its nearest integer:

Math.ceil(4.4);     // returns 5

Math.sin()

Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.

Math.sin(90 * Math.PI / 180);     // returns 1 (the sine of 90 degrees)

Math.cos()

Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.

Math.cos(0 * Math.PI / 180);     // returns 1 (the cos of 0 degrees)

Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:
Math.min(01503020, -8, -200);  // returns -200

Math.random()

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):

Math.random();     // returns a random number

Math Properties (Constants)

JavaScript provides 8 mathematical constants that can be accessed with the Math object:

Math.E        // returns Euler's numberMath.PI       // returns PIMath.SQRT2    // returns the square root of 2Math.SQRT1_2  // returns the square root of 1/2Math.LN2      // returns the natural logarithm of 2Math.LN10     // returns the natural logarithm of 10Math.LOG2E    // returns base 2 logarithm of EMath.LOG10E   // returns base 10 logarithm of E



Share:

Related Posts:

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

Popular Posts

Blog Archive

Blog Archive

Hassan.mosmer1@gmail.com