theonlychase
4/30/2019 - 7:31 PM

Math Methods

// Math.round()
// The Math.round(x) method returns the value of x rounded to the nearest integer.
Math.round(3.4); //  3
Math.round(3.5); //  4

// Math.pow()
// The Math.pow(x, y) method returns the base to the exponent power, that is, baseexponent.
Math.pow(5, 3); // 125

// Math.sqrt()
// The Math.sqrt(x) method returns the square root of x.
let x = Math.sqrt(9);    // 3
let y = Math.sqrt(25);   // 5

// Math.abs()
// The Math.abs(x) method returns the absolute (positive) value of x.
Math.abs(-5);    // 5

// Math.ceil()
// The Math.ceil(x) method returns the value of x rounded up to the next highest integer.
let x = Math.ceil(3.1);    // 4
let y = Math.ceil(3.8);    // 4

// Math.floor()
// The Math.floor(x) method returns the value of x rounded down to the next lowest integer.
let x = Math.floor(3.1);    // 3
let y = Math.floor(3.8);    // 3

// Math.max() and Math.min()
// The Math.max() method returns the number with the highest value in a list of arguments.
Math.max(5, 2, 12, 4, 97, 26);   // 97
// The Math.min() method returns the number with the lowest value in a list of arguments.
Math.min(5, 2, 12, 4, 97, 26);   // 2

// Math.random()
// The Math.random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).
Math.random();

// Math.log()
// Check if a number is a power of 2, or any #
Math.pow(2, Math.round(Math.log(num) / Math.log(2))) === num