In addition to the mathematical constants that can be accessed from the Math object there are also several methods available.

The following example uses the round() method of the Math object to round a number to the nearest integer:

 document.write(Math.round(4.7));

The code above will result in the following output:
5

The following example uses the random() method of the Math object to return a random number between 0 and 1:

 document.write(Math.random());

The code above can result in the following output:
0.6936874867631697 

The following example uses the floor() and random() methods of the Math object to return a random number between 0 and 10:

 document.write(Math.floor(Math.random()*11));

The code above can result in the following output:
0


JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.
You may reference these constants from your JavaScript like this:

Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E


The Math object allows you to perform mathematical tasks.

Math Object

The Math object allows you to perform mathematical tasks.
The Math object includes several mathematical constants and methods.

Syntax for using properties/methods of Math:

 var pi_value=Math.PI;
var sqrt_value=Math.sqrt(16);

Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.



The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).

The Boolean object represents two values: "true" or "false".

The following code creates a Boolean object called myBoolean:

 var myBoolean=new Boolean();

Note: If the Boolean object has no initial value or if it is 0, -0, null, "", false, undefined, or NaN, the object is set to false. Otherwise it is true (even with the string "false")!
All the following lines of code create Boolean objects with an initial value of false:

 var myBoolean=new Boolean();
var myBoolean=new Boolean(0);
var myBoolean=new Boolean(null);
var myBoolean=new Boolean("");
var myBoolean=new Boolean(false);
var myBoolean=new Boolean(NaN);

And all the following lines of code create Boolean objects with an initial value of true:


var myBoolean=new Boolean(true);
var myBoolean=new Boolean("true");
var myBoolean=new Boolean("false");
var myBoolean=new Boolean("Richard");




To modify a value in an existing array, just add a new value to the array with a specified index number:

 myCars[0]="Opel";

Now, the following code line:

 document.write(myCars[0]);

will result in the following output:
Opel


You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.

The following code line:

 document.write(myCars[0]);

will result in the following output:
 
Saab


The following code creates an Array object called myCars:

var myCars=new Array();

There are two ways of adding values to an array (you can add as many values as you need to define as many variables you require).

1:

 var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";

You could also pass an integer argument to control the array's size:

 var myCars=new Array(3);
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";

2:

 var myCars=new Array("Saab","Volvo","BMW");

Note: If you specify numbers or true/false values inside the array then the type of variables will be numeric or Boolean instead of string.