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 type of <input> creates a date input control, such as a pop-up calendar?
type="text" type="date" type="button"
<input type="date" name="set_date" value="2012-10-15" />
Which CSS property adds shadow effects to the text of an element?
font-style color text-shadow
h2 {
  text-shadow: 2px 3px 3px #a0a1fe;
Click on the function that adds new elements to the end of an array.
pop() shift() push()
var pags = ["lessons", "courses"];
pags.push("download", "tutorials");
alert(pags[2]);            // download
Which function sorts an array by key, in ascending order, maintaining key to data correlations?
asort() ksort() sort()
$lang = array(10=>"PHP", 20=>"JavaScript", "site"=>"coursesweb.net");
var_export($lang);     // array ("site"=>"coursesweb.net", 10=>"PHP", 20=>"JavaScript")
The Number and Math Objects

Last accessed pages

  1. Working with getElementById (2265)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (7162)
  3. Convert XML to JSON in PHP (726)
  4. Add and Remove HTML elements and Content with jQuery (4531)
  5. Uploading images to server with Ajax (977)

Top accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (7675)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (7449)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (7162)
  4. PHP Chat Script (6603)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (6556)