Calculate with Math Objects
JavaScript’s Math object can save you a lot of work when performing many sorts of calculations that frequently occur.
Unlike the Date object, the Math object does not need to be created before use; it already exists, and you can call its methods directly.
Some Example:--
Rounding
The methods ceil(), floor(), and round() are useful for truncating the decimal parts of numbers:
var myNum1 = 12.55; var myNum2 = 12.45; alert(Math.floor(myNum1)); // shows 12 alert(Math.ceil(myNum1)); // shows 13 alert(Math.round(myNum1)); // shows 13 alert(Math.round(myNum2)); // shows 12
Finding Minimum and Maximum
We can use min() and max() to pick the largest and smallest number from a list:
var ageMary = 27; var ageChris = 31; var ageSandy = 19; document.write("The youngest person is " + Math.min(ageDavid, ageMary, ageChris, ageSandy) + " years old
"); document.write("The oldest person is " + Math.max(ageDavid, ageMary, ageChris, ageSandy) + " years old
");
The output as written to the page looks like this:
The youngest person is 19 years old The oldest person is 31 years old
Random Numbers
To generate a random number, we can use Math.random(), which generates a random number between 0 and 1.
Normally we like to specify the possible range of our random numbers, for example, we might want to generate a random integer between 0 and 100.
As Math.random() generates a random number between 0 and 1, it’s helpful to wrap it in a small function that suits our needs. The following function takes the Math object’s randomly generated number, scales it up by multiplying by the variable range (passed to the function as an argument), and finally uses round() to remove any fractional part:
function myRand(range) { return Math.round(Math.random() * range); }
To generate a random integer between 0 and 100, we can then simply call
myRand(100);
Mathematical Constants
These constants can be used directly in your calculations:
var area = Math.PI * radius * radius; // area of circle var circumference = 2 * Math.PI * radius; // circumference
The with Keyword
Although you can use the with keyword with any object, the Math object is an ideal object to use an example. By using with you can save yourself some tedious typing.
The keyword with takes an object as an argument, and is followed by a code block wrapped in braces. The statements within that code block can call methods without specifying an object, and JavaScript assumes that those methods belong to the object passed as an argument.
Example:
with (Math) { var myRand = random(); var biggest = max(3,4,5); var height = round(76.35); }
In this example, we call Math.random(), Math.max(), and Math.round() simply by using the method names, because all method calls in the code block have been associated with the Math object.
Try it Yourself: Reading the Date and Time
<!DOCTYPE html> <html> <head> <title>Current Date and Time</title> <style> p {font: 14px normal arial, verdana, helvetica;} </style> <script> function telltime() { var out = ""; var now = new Date(); out += "<br />Date: " + now.getDate(); out += "<br />Month: " + now.getMonth(); out += "<br />Year: " + now.getFullYear(); out += "<br />sections: " + now.getsections(); out += "<br />Minutes: " + now.getMinutes(); out += "<br />Seconds: " + now.getSeconds(); document.getElementById("div1").innerHTML = out; } </script> </head> <body> The current date and time are:<br/> <div id="div1"></div> <script> telltime(); </script> <input type="button" onclick="location.reload()" value="Refresh" /> </body> </html>