JavaScript Math Object
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Example
Math.PI;
The Math Object
Unlike other objects, the Math object has no constructor.
The Math object is static.
All methods and properties can be used without creating a Math object first.
Math Properties (Constants)
The syntax for any Math property is : Math.property
.
JavaScript provides 8 mathematical constants that can be accessed as Math properties:
Example
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Math Methods
The syntax for Math any methods is : Math.method(number)
Number to Integer
There are 4 common methods to round a number to an integer:
Math.round(x) | Returns x rounded to its nearest integer |
Math.ceil(x) | Returns x rounded up to its nearest integer |
Math.floor(x) | Returns x rounded down to its nearest integer |
Math.trunc(x) | Returns the integer part of x |
Math.round()
Math.round(x)
returns the nearest integer:
Examples
Math.round(4.6); Math.round(4.5); Math.round(4.4);
Math.ceil()
Math.ceil(x)
returns the value of x rounded up to its nearest integer:
Example
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Math.floor()
Math.floor(x)
returns the value of x rounded down to its nearest integer:
Example
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Math.trunc()
Math.trunc(x)
returns the integer part of x:
Example
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Math.sign()
Math.sign(x)
returns if x is negative, null or positive:
Example
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.pow()
Math.pow(x, y)
returns the value of x to the power of y:
Example
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
returns the square root of x:
Example
Math.sqrt(64);
Math.abs()
Math.abs(x)
returns the absolute (positive) value of x:
Example
Math.abs(-4.7);
Math.sin()
Math.sin(x)
returns the sine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
returns the cosine (a value between -1 and 1) of the angle x (given in radians).
If you want to use degrees instead of radians, you have to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() and Math.max()
Math.min()
and Math.max()
can be used to find the lowest or highest value in a list of arguments:
Example
Math.min(0, 150, 30, 20, -8, -200);
Example
Math.max(0, 150, 30, 20, -8, -200);
Math.random()
Math.random()
returns a random number between 0 (inclusive), and 1 (exclusive):
Example
Math.random();
You will learn more about Math.random()
in the next chapter of this tutorial.
The Math.log() Method
Math.log(x)
returns the natural logarithm of x.
The natural logarithm returns the time needed to reach a certain level of growth:
Examples
Math.log(1); Math.log(2); Math.log(3);
Math.E and Math.log() are twins.
How many times must we multiply Math.E to get 10?
Math.log(10);
The Math.log2() Method
Math.log2(x)
returns the base 2 logarithm of x.
How many times must we multiply 2 to get 8?
Math.log2(8);
The Math.log10() Method
Math.log10(x)
returns the base 10 logarithm of x.
How many times must we multiply 10 to get 1000?
Math.log10(1000);
JavaScript Math Methods
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
acosh(x) | Returns the hyperbolic arccosine of x |
asin(x) | Returns the arcsine of x, in radians |
asinh(x) | Returns the hyperbolic arcsine of x |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
atanh(x) | Returns the hyperbolic arctangent of x |
cbrt(x) | Returns the cubic root of x |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, …, n) | Returns the number with the highest value |
min(x, y, z, …, n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sign(x) | Returns if x is negative, null or positive (-1, 0, 1) |
sin(x) | Returns the sine of x (x is in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
tanh(x) | Returns the hyperbolic tangent of a number |
trunc(x) | Returns the integer part of a number (x) |
36 Responses
cost levofloxacin order levofloxacin 250mg generic
avodart online order zofran 8mg pill ondansetron 4mg price
buy aldactone online cheap purchase spironolactone pill diflucan usa
purchase ampicillin generic ampicillin 250mg pills erythromycin online buy
cheap fildena 50mg order robaxin 500mg buy robaxin 500mg online cheap
oral sildenafil 100mg aurogra 100mg oral estrace 2mg generic
buy lamotrigine 200mg without prescription minipress 2mg ca buy tretinoin cream
order tadalafil 10mg online cheap buy diclofenac 50mg voltaren over the counter
order isotretinoin 40mg for sale purchase isotretinoin online order generic azithromycin 250mg
buy indocin 50mg generic indomethacin amoxicillin order online
order anastrozole 1mg generic generic biaxin viagra 50mg drug
tadalafil 20mg sans ordonnance en france sildenafil 100mg pas cher acheter 200mg du sildenafil
prednisone 20mg cheap buy viagra for sale cheap sildenafil 50mg
brand ramipril 10mg irbesartan 150mg over the counter azelastine sprayers
clonidine 0.1 mg over the counter meclizine 25 mg over the counter spiriva cost
order buspar pills order oxybutynin 2.5mg generic ditropan brand
order hytrin 5mg online cheap order terazosin 1mg without prescription order sulfasalazine online cheap
alendronate drug paracetamol tablet buy famotidine pills
buy benicar 10mg online generic benicar 10mg acetazolamide without prescription
order tacrolimus 5mg online cheap order ursodiol 300mg pills cheap urso
buy imdur 40mg micardis 80mg oral order micardis 20mg generic
buy zyban 150 mg online cheap order quetiapine 50mg generic quetiapine 100mg drug
order generic molnunat 200 mg order molnunat online cheap lansoprazole oral
order zoloft online cheap order sertraline 50mg generic viagra pills
buy imuran 100mcg generic sildenafil us female viagra sildenafil
tadalafil canada purchase fluoxetine pill purchase sildenafil online cheap
buy tadalafil 10mg pill buy phenazopyridine 200 mg generic symmetrel 100 mg pill
order revia 50mg pill aripiprazole 20mg brand order abilify 20mg pills
dapsone sale aceon tablet perindopril for sale online
provera 5mg without prescription medroxyprogesterone 5mg tablet cyproheptadine pills
provigil over the counter modafinil 100mg over the counter ivermectin 6mg for sale
fluvoxamine price order cymbalta 40mg generic order glucotrol 5mg for sale
order isotretinoin 10mg online cost amoxil 500mg order deltasone 5mg sale
oral nootropil order viagra generic cost viagra 50mg
order azithromycin 250mg pills zithromax without prescription neurontin without prescription
buy cialis 10mg cialis price sildenafil 200 mg