Learn Javascript
Mathematical Functions
Join the Discussion
More of this Tutorial
Reference
Introduction
Javascript provides access to a lot of common mathematical functions as well as some of the more common irrational numbers (e, pi, square root of 2 etc.) using the Math class. This class works a bit differently from other built in classes because the class is statically defined. This means that you don't define objects based on the class, you refer to the methodss and values within the class directly.
Mathematical Values
If you don't know what irrational numbers are then you will probably never need to use any of the values provided by the Math class. If you are going to use Javascript to perform complex mathematical functions then you will not be able to avoid using them. Javascript provides eight common irrational numbers that can be accessed from the Math class. These are:
- e
- pi
- spuare root of 2
- square root of 1/2
- natural log of 2
- natural log of 10
- log of e base 2
- log of e base 10
You reference these from your javascript using the following references respectively:
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Mathematical Methods
In addition to the fixed values that can be accessed from the Math class there are also a whole range of functions that are available. You will probably only use the sin, cos, tan, acos, asin, atan, atan2, log, exp, pow, and sqrt functions if you are performing mathematical functions using Javascript. There are a few other functions that the Math class provides that you will find really useful in many of your Javascripts.
- Math.ceil(var) this call will round up a number to the nearest integer eg. 1.1 will be rounded up to 2
- Math.floor(var) this call will round down a number to the nearest integer eg. 1.9 will be rounded down to 1
- Math.round(var) this call will round a number to the nearest integer eg. 1.9 will be rounded up to 2 and 1.1 will be rounded down to 1
- Math.abs(var) this call will return the absolute value of the number ie if the number is negative then the minus sign will be removed
- Math.max(var1,var2) this call will return the larger of the two numbers passed to it
- Math.min(var1,var2) this call will return the smaller of the two numbers passed to it
There is one more function provided by the Math class though which is more useful than all of the others combined. The Math.random() function returns a random number between 0 and 1.
Using What You Know
We can make use of a couple of the functions provided by the Math class to select one message at random from several supplied messages. Here is an example of the code we need.
mess[0] = 'First message';
mess[1] = 'Second message';
mess[2] = 'Third message';
document.write(
This code will display one of the three messages on the page but which is displayed will depend on the random number that is generated. The random number is multiplied by the number of entries in the array (mess.length) and then rounded down to the nearest integer in order to give a value that directly relates to one of the array entries. With coding it this way we can add more entries to the array and have them included in the selection without having to change the write statement.

