JSFiddle - React, Tailwind, and code Playground
by Nazmus Nasir
HTML
<!-- Easy JavaScript # 34 - Intro to Math Object -->
<p>
Welcome to the 34th Easy JavaScript tutorial, part of <a href="http://www.easyprogramming.net">EasyProgramming.net</a>. We're going to learn about the Math object - capital-M.
</p>
<p>
Not too many people like math, but without math, programming wouldn't exist. And JavaScript does its best to make Math easy by including the Math object to be used.
</p>
<p>
Let's take a look!
</p>
<ul>
<li>Math.PI - constant, returns the value of pi </li>
<li>Math.E - constant, returns the value of Eulers number</li>
<li>Math.ceil(num) - function, returns number rounded up to the nearest integer</li>
<li>Math.floor(num) - function, returns number rounded down to the nearest integer</li>
<li>Math.round(num) - function, returns rounded number to closest integer</li>
<li>Math.abs(num) - function, returns the aboslute value of a number</li>
<li>Math.pow(num,exponent) - function, returns x to the y power (x^y)</li>
<li>Math.sqrt(num) - function, returns square root of a number</li>
<li>Math.min(x,y,z) - function, returns the lowest number in a set of arguments passed, no limit to the number of arguments</li>
<li>Math.max(x,y,z) - function, returns the largest number in a set of arguments passed, no limit to the number of arguments</li>
<li>Math.random() - function, returns a random value between 0 and 1</li>
</ul>
JavaScript
//Constant - does not need parenthesis or arguments/parameters
console.log('Math.PI = ' + Math.PI);
//Constant - does not need parenthesis or arguments/parameters
console.log('Math.E = ' + Math.E);
//rounds to nearest integer (down if less than 5, up if 5 or higher)
console.log('Math.round() = ' + Math.round(53.25));
//always rounds up
console.log('Math.ceil = ' + Math.ceil(53.25));
//always rounds down
console.log('Math.floor = ' +Math.floor(53.25));
//gives the absolute integer value
console.log('Math.abs = ' + Math.abs(-53.25));
//10^4
console.log('Math.pow = ' + Math.pow(10,4));
//square root of 100 = 10
console.log('Math.sqrt = ' + Math.sqrt(100));
//min is -1000!
console.log('Math.min = ' + Math.min(1,3,9,-25,100,-1000));
//max is 100
console.log('Math.max = ' + Math.max(1,3,9,-25,100,-1000));
//random number between 0 and 1
console.log('Math.random = ' + Math.random());