Hey there! Welcome to the 34th Easy JavaScript tutorial, part of EasyProgramming.net. Let's take a brief look at the Math object.
Not too many people like math, but it's fundamental in programming, whether you use it in your code or not. JavaScript provides you with the Math object to make it easier. The Math object starts with the keyword Math, capital-M.
The Math object comes with a few constants like Pi and E and a bunch of functions (aka methods) you can use on your numbers. Let's quickly go through them.
Method/Constant | Description |
---|---|
Math.PI | constant, returns the value of pi |
Math.E | constant, returns the value of Eulers number |
Math.ceil(num) | function, returns number rounded up to the nearest integer |
Math.floor(num) | function, returns number rounded down to the nearest integer |
Math.round(num) | function, returns rounded number to closest integer |
Math.abs(num) | function, returns the aboslute value of a number |
Math.pow(num,exponent) | function, returns x to the y power (x^y) |
Math.sqrt(num) | function, returns square root of a number |
Math.min(x,y,z) | function, returns the lowest number in a set of arguments passed, no limit to the number of arguments |
Math.max(x,y,z) | function, returns the largest number in a set of arguments passed, no limit to the number of arguments |
Math.random() | function, returns a random value between 0 and 1 |
To fork the fiddle and follow along: https://jsfiddle.net/easyjs/1hc4zLza/
Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!