Www.JavaBasics.We.Bs

home 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 Array
JS Boolean
JS Date
JS Math
JS String
JS HTML DOM

Examples/Quiz
JS Basic Examples
JS Core Examples
JS DOM Examples
JS Quiz


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 10
How to find a random number from 0 to 10 using the random() and round() methods.

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.


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

Properties

Syntax: object.property_name

Property Description NN IE 
E Returns the base of a natural logarithm 2 3
LN2 Returns the natural logarithm of 2 2 3
LN10 Returns the natural logarithm of 10 2 3
LOG2E Returns the base-2 logarithm of E 2 3
LOG10E Returns the base-10 logarithm of E 2 3
PI Returns PI 2 3
SQRT1_2 Returns 1 divided by the square root of 2 2 3
SQRT2 Returns the square root of 2 2 3

Methods

Syntax: object.method_name()

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



previous next





Copyright © 2007-2008, Www.JavaBasics.We.Bs All rights reserved.