English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The JavaScript Math object allows you to perform mathematical operations on numbers.
Unlike other global objects, the Math object does not have a constructor. Methods and properties are static.
All methods and properties can be used without first creating a Math object.
Math.PIproperty represents the ratio of the circumference of a circle to its diameter, approximately3.14159.
Math.PI;Test to see‹/›
Note: All properties and methods of the Math object are static and can be called using Math as an object without creating it.
This method roundsxthe value is rounded to the nearest integer.Math.round(x)
If the decimal part of the parameter is greater than 0.5then the parameter will be rounded to the integer with the next higher absolute value.
If less than 0.5then the parameter will be rounded to the integer with the lower absolute value.
If the decimal part is exactly 0.5then the parameter will be rounded along+∞rounds to the nearest integer.
Math.round(3.4); // 3 Math.round(3.5); // 4 Math.round(3.6); // 4 Math.round(-20.2); // -20 Math.round(-20.8); // -21Test to see‹/›
This method returns the base raised to the power of the exponent, i.e., base exponent.Math.pow(x, y)
Math.pow(5, 3);// 125Test to see‹/›
The first parameter is the base.
The second parameter is used to raise the base to the power.
This method returnsx'ssquare root.Math.sqrt(x)
let x = Math.sqrt(9);// 3 let y = Math.sqrt(25); // 5 document.write("SQRT of 9 is: " + x + "<br>" + "SQRT of 25 is: " + y);Test to see‹/›
This method returnsxthe absolute (positive) value.Math.abs(x)
Math.abs(-5);// 5Test to see‹/›
This method returnsrounding.to the next largest integer.xvalue.Math.ceil(x)
let x = Math.ceil(3.1);// 4 let y = Math.ceil(3.8);// 4 document.write(x) + "<br>" + y);Test to see‹/›
This method returnsrounding.to the next smallest integer.xvalue.Math.floor(x)
let x = Math.floor(3.1);// 3 let y = Math.floor(3.8);// 3 document.write(x) + "<br>" + y);Test to see‹/›
Math.sin()The method returns the sine value of the specified number.
This method returns the cosine value of the specified number.-1and1The numerical value between
Math.sin(0); // 0 Math.sin(1); // 0.8414709848078965 Math.sin(4); // -0.7568024953079282 Math.sin(Math.PI); // 1.2246467991473532e-16 Math.sin(Math.PI / 2); // 1Test to see‹/›
Math.cos()The method returns the cosine value of the specified number.
This method returns the cosine value of the specified number.-1and1The numerical value between
Math.cos(1);Test to see‹/›
Math.max()The method returns the number with the largest value in the parameter list.
Math.max(5, 2, 12, 4, 97, 26); // 97Test to see‹/›
Math.min()The method returns the number with the smallest value in the parameter list.
Math.min(5, 2, 12, 4, 97, 26); // 2Test to see‹/›
Math.random()The method returns a random number from 0 (inclusive) to1a random number (excluding).
Math.random();Test to see‹/›
You willMath.random()More information can be found in the next chapter.
For a complete reference of properties and methods, please visit ourJavaScript Math Object Reference Manual.
The reference section includes descriptions and examples of all Math properties and methods.