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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Math object - Methods for mathematical operations

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)