SITE SEARCH

SITE LINKS

    • Js Basic
  • JS HOME
  • JS Intro
  • JS HOW TO
  • JS Where To
  • JS Statements
  • JS Comments
  • JS Variables
  • JS Operators
  • JS Comparisons
  • JS If...Else
  • JS Switch
  • JS Popup Boxes
  • JS Functions
  • JS For Loop
  • JS While Loop
  • JS Break Loop
  • JS For..In
  • JS Events
  • JS Try...Catch
  • JS Throw
  • JS Special Text
  • JS Guidelines

  • JS Objects
  • JS Object Intro
  • JS String
  • JS Date
  • JS Array
  • JS Boolean
  • JS Math
  • JS RegExp
  • JS Html DOM

  • JS advanced
  • Js Browser
  • JS Cookies
  • JS Validation
  • JS Animation
  • JS Image Maps
  • JS Timing
  • JS Create Objects
  • JS Summary

Home Html Css Javascript Php Asp .Net Sql Xml
user login New user? Register Here | Forgot Password?

JavaScript Math Object

Prev
Next
The Math object allows you to perform mathematical tasks.

Math Object

The Math object allows you to perform mathematical tasks.

The Math object includes several mathematical constants and methods.

Syntax for using properties/methods of Math:

var pi_value=Math.PI;
var sqrt_value=Math.sqrt(16);

Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.

Mathematical Constants

JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.

You may reference these constants from your JavaScript like this

Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E

Mathematical Methods

In addition to the mathematical constants that can be accessed from the Math object there are also several methods available.

The following example uses the round() method of the Math object to round a number to the nearest integer:

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

The code above will result in the following output:

5

The following example uses the random() method of the Math object to return a random number between 0 and 1:

document.write(Math.random());

The code above can result in the following output:

0.30512820005022656

The following example uses the floor() and random() methods of the Math object to return a random number between 0 and 10:

document.write(Math.floor(Math.random()*11));

The code above can result in the following output:

1

Prev
Next
Top

 

 

© 2010 Copyrighted. Yavum.com™