1. The Number object

The Number object is another predefined JavaScript object that offers some useful properties and methods for use with numbers.

Properties of the Number object

- Example:
In the following JS script we create a new property (mult8) to the Number object, this new property multiplies by 8 the value of a variable.
<script type="text/javascript"><!--
var nr = 7;

// creates a new property ( mult8 ) to the Number object
Number.prototype.mul8 = nr*8;

// uses the property "mult8"
document.write(nr.mul8);       // 65

Methods of the Number Object

2. The Math object

The Math object is a predefined JavaScript object used for mathematical purposes, it provides constants and functions for more complex operations.
If for example we want to get a random number between 0 and 1, we write:

Properties of the Math object

All of the properties of the Math object contain read-only values, that can be useful in mathematical calculations (They must be written with capital letters). - Example:
<script type="text/javascript"><!--
// find the area of a circle,
var rad = 7.8;           // radius value
var area = Math.PI * (rad * rad);     // gets the area
document.write(area);        // 191.134497044403

Methods of the Math Object

The methods of the Math object enable you to perform certain calculations in your scripts.
- Some examples:
1) Alerts the value of the positive square root of 64, which is 8.
<script type="text/javascript"><!--
alert(Math.sqrt(64));         // 8

2) Returns the number with the highest value, between 8, 4, 88, 56
<script type="text/javascript"><!--
alert(Math.max(8, 4, 88, 56));       // 88

3) The following code would return the value of 4 to the 3rd power
<script type="text/javascript"><!--
alert(Math.pow(4, 3));       // 64

- Math.random() only returns numbers between zero and one. If you want to generate a random integer between two integers, you can combine the methods "round()" and "random()", as shown in the following example.
<script type="text/javascript"><!--
// gets an random integer between 1 and 10
var lower = 1;
var higher = 10;
var nr = Math.round(Math.random() * (higher - lower)) + lower;


Daily Test with Code Example

Which tag is used in <table> to create table header cell?
<thead> <th> <td>
  <th>Title 1</th>
  <th>Title 2</th>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
The Number and Math Objects

Last accessed pages

  1. DirectoryIterator to get file and directory info (1623)
  2. Zoomooz - Make Web Page Elements Zoom (1996)
  3. Bloons Tower Defense 5 (518)
  4. Convert XML to JSON in PHP (6239)
  5. List with All the Games (6289)

Popular pages this month

  1. Bubbles3 (3397)
  2. Butterfly Kyodai (3012)
  3. Zuma Deluxe (2212)
  4. Qwop (1432)
  5. Backgammon (1307)