bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Math Object

The Math Object

The JavaScript Math object allows you to perform mathematical tasks.

The Math object is static.

All methods and properties can be used without creating a Math object first.

Math.PI;

Math Properties (Constants)

The syntax for any Math property is : Math. property .

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

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

Math Methods

The syntax for Math any methods is : Math. method ( number )

Number to Integer

There are 4 common methods to round a number to an integer:

Math.round(x)Returns x rounded to its nearest integer
Math.ceil(x)Returns x rounded up to its nearest integer
Math.floor(x)Returns x rounded down to its nearest integer
Math.trunc(x)Returns the integer part of x ( new in ES6 )

Math.round()

Math.round(x) returns the nearest integer:

Math.round(4.6);

Math.ceil()

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

Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);

Math.floor()

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

Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);

Math.trunc()

Math.trunc(x) returns the integer part of x:

Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);

Math.sign()

Math.sign(x) returns if x is negative, null or positive.

  • If x is positive it returns 1
  • If x is negative it returns -1
  • If x is zero, it returns 0
Math.sign(-4);
Math.sign(0);
Math.sign(4);

Math.trunc() and Math.sign() were added to JavaScript 2015 - ES6 .

Math.pow()

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

Math.pow(8, 2);

Math.sqrt()

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

Math.sqrt(64);

Math.abs()

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

Math.abs(-4.7);

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(0, 150, 30, 20, -8, -200);
Math.max(0, 150, 30, 20, -8, -200);

Math.random()

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

Math.random();

You will learn more about Math.random() in the next chapter of this tutorial.

The Math.log() Method

Math.log(x) returns the natural logarithm of x.

The natural logarithm returns the time needed to reach a certain level of growth:

Math.log(1);

Math.E and Math.log() are twins.

Math.log(10);

The Math.log2() Method

Math.log2(x) returns the base 2 logarithm of x.

Math.log2(8);

The Math.log10() Method

Math.log10(x) returns the base 10 logarithm of x.

Math.log10(1000);

Learn More

Full Math Reference

Previous

JavaScript Maps

Next

JavaScript RegExp

This chapter

Overview
37

Lessons

216m

Read time

1. JavaScript Strings2. JavaScript Numbers3. JavaScript Dates4. JavaScript Arrays5. JavaScript Sets6. JavaScript Maps7. JavaScript Math Object8. JavaScript RegExp9. JavaScript String Templates10. JavaScript Number Methods11. JavaScript Date Formats12. JavaScript Array Methods13. JavaScript Set Methods14. JavaScript Map Methods15. JavaScript Random16. JavaScript RegExp Flags17. JavaScript String Methods18. JavaScript Number Properties19. JavaScript Get Date Methods20. JavaScript Array Search21. JavaScript Set Logic22. JavaScript WeakMap23. RegExp Character Classes24. JavaScript String Search25. JavaScript Bitwise Operations26. JavaScript Set Date Methods27. JavaScript Array Sort28. JavaScript WeakSet29. RegExp Meta Characters30. JavaScript BigInt31. JavaScript Array Iterations32. Regular Expression Assertions33. JavaScript Array Const34. RegExp Quantifiers35. JavaScript RegExp Patterns36. JavaScript RegExp Objects37. RegExp Methods

On this page

The Math ObjectMath Properties (Constants)Math MethodsNumber to IntegerMath.round()Math.ceil()Math.floor()Math.trunc()Math.sign()Math.pow()Math.sqrt()Math.abs()Math.sin()Math.cos()Math.min() and Math.max()Math.random()The Math.log() MethodThe Math.log2() MethodThe Math.log10() Method