The Math contains properties and methods for constants and mathematical operations.
The Math object is applied directly, followed by property or method.
- Example:
const x = Math.PI; //value of PI
var y = Math.sqrt(16); //square root of 16

- Example, a function that returns a random number between two integers.
<script>
//returns a random number between min and max (inclusive)
function randomInt(min, max){
 min = Math.ceil(min);
 max = Math.floor(max);
 return Math.floor(Math.random() *(max - min + 1)) +min; 
}

var nr1 = randomInt(1, 10); //number between 1 and 10
var nr2 = randomInt(-10, 20); //number between -10 and 20

document.write('<br> Random number between 1 and 10: '+ nr1 +'<br> Random number between -10 and 20: '+ nr2);
</script>

Extending Math Object

The Math object can be extended, you can add custom properties and methods using this syntax:

//defining property
Math.prop_name = prop_val;

//defining method
Math.method_name = function(){
 //code of method
}

- The following example adds a method to the Math object for calculating the greatest common divisor of a list of arguments.
<script>
//returns the greatest common divisor of a list of arguments
Math.cmmdc = function() {
 if(arguments.length ==2){
 if(arguments[1] ==0) return arguments[0];
 else return Math.cmmdc(arguments[1], arguments[0] % arguments[1]);
 }
 else if(arguments.length >2){
 var result = Math.cmmdc(arguments[0], arguments[1]);
 for(var i=2; i<arguments.length; i++) result = Math.cmmdc(result, arguments[i]);
 return result;
 }
};

let mdv = Math.cmmdc(20, 30, 15, 70, 40); // 5
document.write('<br> The greatest common divisor of the numbers: (20, 30, 15, 70, 40) is: '+ mdv);
</script>

Properties of the Math object

Math object properties represent mathematical constants.


Methods of the Math object

Math.abs(x) - returns the absolute value of.
var n1 = 18;
var n2 = -25.3;

console.log(Math.abs(n1)); // 18
console.log(Math.abs(n2)); // 25.3
Math.acos(x) - returns the arccosine of x, in radians.
var n1 = -1;
var n2 = 0.5;

console.log(Math.acos(n1)); // 3.141592653589793
console.log(Math.acos(n2)); // 1.0471975511965979
Math.asin(x) - returns the arcsine of x, in radians.
var n1 = -1;
var n2 = 0.5;

console.log(Math.asin(n1)); // -1.5707963267948966 (-pi/2)
console.log(Math.asin(n2)); // 0.5235987755982989
Math.atan(x) - the arctangent of x, in radians.
var n1 = 1;
var n2 = 0.5;

console.log(Math.atan(n1)); // 0.7853981633974483
console.log(Math.atan(n2)); // 0.4636476090008061
Math.atan2(y, x) - returns the arctangent of the quotient of its arguments (x,y).
var v1 = Math.atan2(90, 15);
var v2 = Math.atan2(15, 90);

console.log(v1); // 1.4056476493802699
console.log(v2); // 0.16514867741462683
Math.cbrt(x) - returns the cube root of a number.
var n1 = 64;

console.log(Math.cbrt(n1)); // 4
Math.ceil(x) - returns the smallest integer greater than or equal to x.
var n1 = 18.7;
var n2 = -25.3;

console.log(Math.ceil(n1)); // 19
console.log(Math.ceil(n2)); // -25
Math.cos(x) - the cosine of the number x, in radians.
var n1 = 0;
var n2 = 1;

console.log(Math.cos(n1)); // 1
console.log(Math.cos(n2)); // 0.5403023058681398
Math.exp(x) - returns the value of E to the power of the x.
var n1 = 2;
var n2 = -1;

console.log(Math.exp(n1)); // 7.38905609893065
console.log(Math.exp(n2)); // 0.36787944117144233
Math.floor(x) - returns x, rounded downwards to the nearest integer.
var n1 = 18.7;
var n2 = -25.3;

console.log(Math.floor(n1)); // 18
console.log(Math.floor(n2)); // -26
Math.hypot(x1, x2, ...) - returns the square root of the sum of squares of its arguments x1, x2, ...
var n1 = 3;
var n2 = 4;

console.log(Math.hypot(n1, n2)); // 5
Math.log(x) - the natural logarithm of x.
var n1 = 1;
var n2 = 10;

console.log(Math.log(n1)); // 0
console.log(Math.log(n2)); // 2.302585092994046
Math.log10(x) - returns the base 10 logarithm of a number.
var n1 = 100000;
var n2 = 2;

console.log(Math.log10(n1)); // 5
console.log(Math.log10(n2)); // 0.3010299956639812
Math.max(x1, x2, ...) - returns the number with the highest value.
var n1 = 61.5;
var n2 = -65.3;
var n3 = 53;

console.log(Math.max(n1, n2, n3)); // 61.5
Math.min(x1, x2, ...) - returns the number with the lowest value.
var n1 = 61.5;
var n2 = -65.3;
var n3 = 53;

console.log(Math.min(n1, n2, n3)); // -65.3
Math.pow(x, y) - the value of 'x' raised to the power of 'y'.
var x = 4;
var y = 3;

console.log(Math.pow(x, y)); // 64
Math.random() - a random number between 0 and 1.
document.write('<br> - Random number between 0 and 1: '+ Math.random());
Math.round(x) - returns the value of the number x rounded to the nearest integer.
var n1 = 5.8;
var n2 = -25.38;

console.log(Math.round(n1)); // 6
console.log(Math.round(n2)); // -25
Math.sin(x) - the sine of x (x is in radians).
var n1 = 0;
var n2 = 1;
var n3 = Math.PI /2;

console.log(Math.sin(n1)); // 0
console.log(Math.sin(n2)); // 0.8414709848078965
console.log(Math.sin(n3)); // 1
Math.sqrt(x) - returns the square root of x.
var n1 = 4;
var n2 = 81.25;

console.log(Math.sqrt(n1)); // 2
console.log(Math.sqrt(n2)); // 9.013878188659973
Math.tan(x) - the tangent of an angle x (x in radians).
var n1 = 1;
var n2 = 90;

console.log(Math.tan(n1)); // 1.5574077246549023
console.log(Math.tan(n2)); // -1.995200412208242
Math.trunc(x) - returns the integer part of the number x, removing decimals.
var n1 = 18.78;
var n2 = -25.3;

console.log(Math.trunc(n1)); // 18
console.log(Math.trunc(n2)); // -25

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Math object - Methods for mathematical operations

Last accessed pages

  1. querySelector and querySelectorAll (9258)
  2. Add, Change, and Remove Attributes with jQuery (37375)
  3. Insert, Select and Update NULL value in MySQL (36877)
  4. Read Excel file data in PHP - PhpExcelReader (65674)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (57523)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (2415)
  2. Contact page - CoursesWeb (2334)
  3. Tabs effect with CSS (2325)
  4. Insert, Select and Update NULL value in MySQL (1315)
  5. PHP getElementById and getElementsByTagName (904)