About
In this code snippet, we’ll check out some of the Math class functions in Javascript.
More information about all the functions and constants here.
Let’s see the code example below.
Code:
//You can get constants from the Math class. let pi = Math.PI; //Display a certain amount of decimal places. console.log(pi); console.log(pi.toPrecision(3)); //Round number. console.log(Math.round(pi)); //Round up. console.log(Math.ceil(pi)); //Round down. console.log(Math.floor(pi)); //Get the absolute value of a number. console.log(Math.abs(-10)); //Do exponentiation. 2^8 console.log(Math.pow(2,8)); //Get squre root of a number. console.log(Math.sqrt(9)); //Gets base 2 log of 256. console.log( (Math.log(256)/Math.log(2)) ); //Trigonometric functions. console.log(Math.sin(90)); console.log(Math.cos(90))