Javascript Math object

The Math object holds a set of constants and methods enabling more complex mathematical operations than the basic arithmetic operators

var root = Math.sqrt(10);


Constants Provided by the Math Object
Property
Description
Math.E
The base of the natural logarithm (Euler's constant e)
Math.LN2
Natural log of 2
Math.LN10
Natural log of 10
Math.LOG2E
Log (base 2) of e
Math.LOG10E
Log (base 10) of e
Math.PI
Pi (p)
Math.SQRT1_2
Square root of 0.5 (equivalently, one over the square root of 2)
Math.SQRT2
Square root of 2
Table 7-4: Methods Provided by the Math Object
Method
Returns
Math.abs(arg)
Absolute value of arg
Math.acos(arg)
Arc cosine of arg
Math.asin(arg)
Arc sine of arg
Math.atan(arg)
Arc tangent of arg
Math.atan2(y, x)
Angle between the x axis and the point (x, y), measured counterclockwise (like polar coordinates). Note how y is passed as the first argument rather than the second.
Math.ceil(arg)
Ceiling of arg (smallest integer greater than or equal to arg)
Math.cos(arg)
Cosine of arg
Math.exp(arg)
e to arg power
Math.floor(arg)
Floor of arg (greatest integer less than or equal to arg)
Math.log(arg)
Natural log of arg (log base e of arg)
Math.max(arg1, arg2)
The greater of arg1 or arg2
Math.min(arg1, arg2)
The lesser of arg1 or arg2
Math.pow(arg1, arg2)
arg1 to the arg2 power
Math.random()
A random number in the interval [0,1]
Math.round(arg)
The result of rounding arg to the nearest integer. If the decimal portion of arg is greater than or equal to .5, it is rounded up. Otherwise, arg is rounded down.
Math.sin(arg)
Sine of arg
Math.sqrt(arg)
Square root of arg
Math.tan(arg)
Tangent of arg
There are several aspects of the Math object that need to be kept in mind. The trigonometric methods work in radians, so you need to multiply any degree measurements by p / 180 before using them. Also, because of the imprecise characteristic of floating-point operations, you might notice minor deviations from the results you expect. For example, though the sine of p is 0, the following code:
alert(Math.sin(Math.PI));
Related Posts:
  • JavaScript String Split Function split()The split() method splits (for lack of a better word) a string up into substrings and returns them in an array. It accepts a string or regular expression argument containing the delimiter at which the string will be b… Read More
  • Javascript String Constructor String is the built-in object corresponding to the primitive string data type. It contains a very large number of methods for string manipulation and examination, substring extraction, and even conversion of strings to marke… Read More
  • JavaScript System Dialogs alert(),confirm(),prompt() The browser is capable of invoking system dialogs to display to the user through the alert(),confirm(), and prompt() methods. These dialogs are not related to the web page being displayedin the browser and do not contain HTM… Read More
  • Javascript search search(regexp)The search() method is the same as indexOf() except that it takes aregular expression pattern instead of a substring. It also returns -1 if thepattern isn’t found."hello world".search(/[aeiou]/); // Find the fi… Read More
  • Javascript Making a simple MouseOver <a href="http://www.cit.cornell.edu"onMouseOver="document.logo.src='family.gif ' ; "onMouseOut ="document.logo.src='harry.gif ' ; " ><img name="logo" src="harry.gif " border=0></a> The onMouseOver and onM… Read More
  • Javascript Image Objects Properties of Image Objects Property Description align Indicates the alignment of the image, usually “left” or “right.” alt The alternative text rendering for the image as set by the alt attribu… Read More
  • Creating A Function In Javascript The basic structure of a function looks like this: function functionName() {// the JavaScript you want to run} The keyword function lets the JavaScript interpreter know you’re creating a function—it’s similar to how you use … Read More
  • Javascript Objects as Arguments You may want to write functions that take many arguments, some ofwhich are optional.function drawElement( color, border, width, height,left, top, zIndex) {// Make and display an element with these variables}drawElement("red"… Read More
  • Javascript The + Operator The binary + operator adds numeric operands or concatenates string operands: 1 + 2 // => 3"hello" + " " + "there" // => "hello there""1" + "2" // => "12" When the values of both operands are numbers, or are both str… Read More
  • Javascript Recursion Recursion is when a function calls itself. This is often useful in mathematics,such as fi nding the nth number in the Fibonacci series (1, 2, 3, 5,8, 13, 21…).function fi bonacci(n) {if ( n < 2 ) {return 1;} else {return … Read More
  • JavaScript and XML To demonstrate JavaScript, XML, and the DOM in action, let’s use Internet Explorer 5.5 or better to load an XML document containing our employee directory and see if we can manipulate it. First, to load in the document w… Read More
  • JavaScript Popups <head><script type="text/javascript">function testPopup() {window.open( "http://www.google.com/" )}</script></head><body><form><input type="button" onClick="testPopup()" value="Click"&g… Read More
  • JavaScript Redirect <script type="text/javascript"> window.location = "http://www.yoursite.com/" </script> … Read More
  • JavaScript Variables Variables store a value you can refer to later in the script. Variable namescan be nearly any valid identifi er. A JavaScript identifi er is a word thatcontains only letters, numbers, $, and _, and that doesn’t start with an… Read More
  • JavaScript Alert The JavaScript alert is a dialogue box that pops up and takes the focus away from the current window and forces the web browser to read the message. 1. <form> <input type="button" onclick= "alert('Are you sure you… Read More