Bookmark Forum | Blog |

# JavaScript Tutorial >> JavaScript Math Object and Properties.

Back             Next
The JavaScript Math object allows us to perform common mathematical tasks such as addition, subtraction, multiplication, division, etc.. The JavaScript Math object includes several mathematical values and functions. We do not need to define the Math object before using it.  Below JavaScript Math object's syntax is the following

## Syntax of JavaScript Math Object

```
<script type = "text/javascript">

Math.propertyname

//or:

Math.methodname(parameters)

</script>

```

## JavaScript Math Methods

Although most of the methods used by Math are self-evident-such as using Math.sin to calculate the sine function of a number-they are summarized in the following sections, with examples for your reference.

## JavaScript Math Object Methods and Properties List

 JavaScript Math Object Method Name Example of JavaScript Math Object Returned Value of Math Object Examples abs Math.abs(-79) 79 acos Math.acos(.5) 1.047197551196597 631 asin Math.asin(1) Math.ceil(7.6) 1.570796326794896 558 8 cos Math.cos(.4) 0.9210609940028851 028 atan Math.atan(.5) 0.4636476090008060 935 ceil exp Math.exp(8) 2980.957987041728 302 floor Math.floor(8.9) 8 log Math.log(5) 1.60943791243410 0282 max Math.max(1, 700) 700 min Math.min(1, 700) 1 pow Math.pow(6,2) 36 random Math.random() .7877896 (varies) round Math.round(.567) 1 sin Math.sin(Math.PI) 0 sqrt Math.sqrt(9801) 99 tan Math.tan(1.5*Math.PI) INF (infinity)

### JavaScript Math.abs Method

document.write(Math.abs(-1));
//JavaScritp Math.abs returns the absolute value of its numeric argument.
//For example, the above will return 1

### JavaScript acos, asin, atan, cos, sin, tan Math Methods

document.write(Math.acos(-1));
//JavaScript Math.acos, Math.asin, Math.atan, Math.cos, Math.sin, Math.tan.
//These return that trig function in radians.
//For example, the above will return 3.141592653589793116

### JavaScript Math.ceil Method

document.write(Math.ceil(14.49999999));
//JavaScript Math.ceil returns the smallest integer
//greater than or equal to the argument passed to it.
//This is equivalent to always rounding up to the nearest integer.
//For example, the above example will return 15:

### JavaScript Math.exp Method

document.write(Math.exp(5));
//JavaScript Math.exp returns the power of its argument.
//For example, the above Example will return 148.4131591025765999

### JavaScript Math.floor Method

document.write(Math.floor(1.9999999));
//JavaScript Math.floor returns the greatest integer less than or
//equal to the value passed to it. This is equivalent to always
//rounding down to the nearest integer.
//For example, the above example will return 1

### JavaScript Math.log Method

pie = Math.PI;
pielog = Math.log(pie);
document.write("The log to the base e of PI is: " + pielog + " .");

//JavaScript Math.log returns the natural log of the argument passed to it.
//For example, the above example will return the log to the base e of pi,
//which is 1.144729885849400164

### JavaScript max, min Math Methods

with (Math) {
document.write("Between Euler's constant and
PI, " + max(E,PI) + " is greater.")
}

//JavaScript Math.max, Math.min.
//Given two numbers, max returns the greater of the two,
//and min returns the lesser of the two

### JavaScript Math.pow Method

document.write(Math.pow(10,4));

//JavaScript Math.pow given a base and an exponent,
//this returns the base to the exponent power.
//For example, the above example return 10000

### JavaScript Math.random Method

document.write(Math.random());

//below lines of random number generation in JavaScript
//will generate any number from 0 to 9
var ranNum= Math.floor(Math.random()*10);
document.write(ranNum);

//JavaScript Math.random method returns a pseudo-random number between 0 and 1.
//For example, the above example might return 0.09983341664682815475:

### JavaScript Math.round Method

document.write(Math.round(1.4999999999));

//JavaScript Math.round method returns the argument rounded to
//the nearest integer. It rounds up if the fractional part of
//the number is .5 or greater, and down otherwise.
//For example, the above example will return 1

### JavaScript Math.sqrt Method

document.write(Math.sqrt(144));

//JavaScript Math.sqrt returns the square root of its argument. The argument must
//be non-negative. otherwise, the sqrt method returns 0.
//For example, the following returns 12

## JavaScript Math Properties

The JavaScript Math object provides us with a few constants that we can use for various scientific and algebraic functions in JavaScript. Note that these JavaScript Math properties are constants and cannot be changed by JavaScript. The following is a summary with each JavaScript Math property's approximate values.
• E
Euler's constant, the base of natural algorithms. Approximately 2.718.
• LN2
The natural log of 2. Approximately 0.693.
• LN10
The natural log of 10. Approximately 2.302.
• PI
The ratio of the circumference of a circle to its diameter. Approximately 3.1416.
• SQRT1_2
The square root of .5 (one half) or one over the square root of 2. Approximately 0.707.
• SQRT2
The square root of 2. Approximately 1.414.

## JavaScript Math Object Properties List

 JavaScript Math Property Name Example  of JavaScript Math Property Returned Value of JavaScript Math Property E Math.E*5 (2.718281828459045091) 13.59140914229522501 LN10 Math.LN10/6 (2.302585092994045901) 0.3837641821656743168 LN2 Math.LN2-Math.E (0.69314718055994529) -2.025134647899099694 PI Math.sin(2*Math.PI/4)1 3.141592653589793116 SQRT2 1/Math.SQRT2 0.7071067811865474617

Back             Next

## JavaScript Math Object Functions and Properties Examples

 [ About ] [ Contact ] [ Home ] [ Links ] [ Site Map ] [ Services ] [ Privacy ]
 Copyright © 2005 -  2019 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 137 visitors are online now