Javascript Math Object functions


Object : Math Methods

Math object has the following methods that can be
used to do specific & basic operations like getting random numbers, rounding
a number, square root calculation, log calculation etc.. in javascript

Here is the reference showing these functions




Function

Example Code

Result
Description

abs(value)

Math.abs(2.822);

document.write(Math.abs(2.822));
2.822
abs()
method returns the absolute value of the argument passed.

ceil(value)

Math.ceil(3.44);

document.write(Math.ceil(3.44)); 4

ceil()
method returns the nearest greater integer value of the value passed as
argument. The argument can be an integer or float.

floor(value)

Math.floor(2.44);

document.write(Math.floor(2.44)); 2

floor()
method returns the nearest lowest (least) integer value of the argument
passed.

log(value)

Math.log(3);
document.write(Math.log(3)); 1.0986122886681098

log(x)
function returns the natural logarithm of the value used.

max(value1,
value2)

Math.max(14,
12);

document.write(Math.max(14, 12)); 14

Math.max(value1,
value2) compares the two values and returns the maximum value of the two
values passed.

min(value1,
value2)

Math.min(14,
12);

document.write(Math.min(14, 12)); 12

Math.min(value1,
value2) compares the two values and returns the minimum value of the two
values passed.

pow(value1, value2)

Math.pow(4,
2);

document.write(Math.pow(4, 2)); 16

Math.pow(value1,
value2) function is used to calculate x y calculation. Here it is 4 2 .

random()

Math.random()

document.write(Math.round(Math.random()*100)); 4

Math.random()
gives a random value between 0 and 1.

round()

Math.round(9.678);

document.write(Math.round(9.678)); 10

Math.round()
function is used to round a float (decimal) value to the nearest integer
value. i.e. round of 4.5 will give 5, rounding of
4.4 will give 4.


0 comments :: Javascript Math Object functions

Post a Comment