Javascript Course


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 meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Math object - Methods for mathematical operations

Last accessed pages

  1. PHP Unzipper - Extract Zip, Rar Archives (31816)
  2. Arrays in ActionScript 3 (3091)
  3. String Object (667)
  4. Register and show online users and visitors (39428)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26057)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (202)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (52)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide