|
The Math object
We all know that Math is very important in the world of programming, so this tutorial will all be the properties and methods of the Math object. It can be used as a reference or to learn more about the Math Object, now some basic knowledge of JavaScript is required. And you must know what object, method, paramater, and property are and how to use them in JavaScript. Here we will discuss only the commonly, and slightly commonly used ones, the VERY uncommonly used ones will be listed at the bottom. And these are all the properties and methods of the Math object that are cross-browser compatible.
First lets look at the methods of the Math object.
abs()
Abs stands for Absolute Value, meaning any number you put in comes out positive.
document.write(Math.abs(2))
document.write(Math.abs(-2))
Will return the same thing.
ceil()
ceil() rounds up. for example
document.write(Math.ceil(10.01))
document.write(Math.ceil(10.99))
Will both return 11 Because it is rounded up.
cos()
cos() stands for cosine. Something like:
document.write(Math.cos(100))
would return the cosine of a hundred
floor()
Opposite of ceil(), always rounds down to the next lowest number. For example
document.write(Math.floor(10.01))
document.write(Math.floor(10.99))
would both return 10. Because they are rounded down.
max()
max() returns the maximum of two numbers, for example
document.write("The largest number is.... " + Math.max(100,200))
would return "The largest number is.... 200" because 100 and 200 were passes in as paramaters and 200 is greater, useless you think right? You're thinking "Hey I know 200 is greater than 100!" Well when you have to compare two variable it's very important.
min()
Opposite of max, returns the smaller number.
pow()
Used instead of ^. If you want to have a number to the something power than do:
Math.pow(2,3)
Would return 8, because 2 to the 3rd power is 8
round()
round() is a combination of floor() and ceil(). If the decimal is greater than or equal to .5 then it will round up, else it will round down.
random()
Math.random() returns a random number between 0 and 1.
sin()
sin() stands for sine. you pass in a number or variable as a paramter and it will return the sine of it, for example:
document.write(Math.sin(100));
would return the sine of 100
sqrt()
sqrt() stands for square root, pass in a number or variable or number as a paramater and it will return the number's square root, for example
document.write(Math.sqrt(100));
would return 10 because 10 is the square root of 100
tan()
tan() stands for tangent, to get the tangent of a number just pass in a number as a paramater. for example to get the tangent of 100 just do:
document.write(Math.tan(100))
Now lets look at the Properties of the Math object
E
Returns Euler's constant.
PI
Returns pi...
SQRT1_2
Returns the Square root of one half
SQRT2
Returns the Square root of the Square root of 2
Here are the "EXTREMELY" unused ones...
Properties:
LN2 - Returns the Natural logarithm of 2
LN10 - Returns the Natural logarithm of 10
LOG2E - Return the base 2 logarithm of E
LOG10E - Return the base 10 logarithm of E
Methods:
acos() - Returns the arccosine of a number
asin() - Returns the arcsine of a number
atan() - Returns the arctangent of a number
exp() - Returns E to the X power. Where X is the paramater
log() - Returns the natural logarithm of a number
|