HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS References
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window Object
JS Frame Object
JS Form Object
JS Browser Object

Examples/Quiz
JS Examples
JS Quiz Test
Online Degrees
Please visit our sponsors !

JavaScript Math Object

previous next

The built-in Math object includes mathematical constants and functions.


Examples

Round
How to round a specified number to the nearest whole number

Random number
The random method returns a random number between 0 and 1

Random number from 0 to 9
How to find a random number from 0 to 9 using the random method.

Max number
How to test which of two numbers, has the higher value. 

Min number
How to test which of two numbers, has the lower value.


The Math object

The built-in Math object includes mathematical constants and functions. You do not need to create the Math object before using it.

To store a random number between 0 and 1 in a variable called "r_number":

r_number=Math.random()

To store the rounded number of 8.6 in a variable called "r_number":

r_number=Math.round(8.6)

The Math object's properties and methods are described below:

NN: Netscape, IE: Internet Explorer, ECMA: Web Standard

Properties Explanation NN IE  ECMA
E Returns the base of a natural logarithm 2.0 3.0 1.0
LN2 Returns the natural logarithm of 2 2.0 3.0 1.0
LN10 Returns the natural logarithm of 10 2.0 3.0 1.0
LOG2E Returns the base-2 logarithm of E 2.0 3.0 1.0
LOG10E Returns the base-10 logarithm of E 2.0 3.0 1.0
PI Returns PI 2.0 3.0 1.0
SQRT1_2 Returns 1 divided by the square root of 2 2.0 3.0 1.0
SQRT2 Returns the square root of 2 2.0 3.0 1.0

Methods Explanation NN IE ECMA
abs(x) Returns the absolute value of x 2.0 3.0 1.0
acos(x) Returns the arccosine of x 2.0 3.0 1.0
asin(x) Returns the arcsine of x 2.0 3.0 1.0
atan(x) Returns the arctangent of x 2.0 3.0 1.0
atan2(x,y) Returns the angle from the x axis to a point 2.0 3.0 1.0
ceil(x) Returns the nearest integer greater than or equal to x 2.0 3.0 1.0
cos(x) Returns the cosine of x 2.0 3.0 1.0
exp(x) Returns the value of E raised to the power of x 2.0 3.0 1.0
floor(x) Returns the nearest integer less than or equal to x 2.0 3.0 1.0
log(x) Returns the natural log of x 2.0 3.0 1.0
max(x,y) Returns the number with the highest value of x and y 2.0 3.0 1.0
min(x,y) Returns the number with the lowest value of x and y 2.0 3.0 1.0
pow(x,y) Returns the value of the number x raised to the power of y 2.0 3.0 1.0
random() Returns a random number between 0 and 1 2.0 3.0 1.0
round(x) Rounds x to the nearest integer 2.0 3.0 1.0
sin(x) Returns the sine of x 2.0 3.0 1.0
sqrt(x) Returns the square root of x 2.0 3.0 1.0
tan(x) Returns the tangent of x 2.0 3.0 1.0


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

Domain Name Registration
Domain Name
Registration & More!



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search