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
--></script>

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
--></script>

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
--></script>

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
--></script>

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

- 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;

document.write(nr);
--></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Click on the tag that creates a cell in table
<tr> <span> <td>
<table></tr>
  <td>Cell-1</td><td>Cell-2</td>
</tr></table>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
}
What instruction can be used to parse all the array items?
for() array() object()
var arr = [1, "ab", "CoursesWeb.net"];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr = array("k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
The Number and Math Objects

Last accessed pages

  1. Creating objects in ActionScript (515)
  2. Flash Tutorials ActionScript 3 (54)
  3. Adding Filters effects (24)
  4. A simple script ActionScript 3 (210)
  5. Using file_put_contents, file_get_contents, readfile and file() (345)

Top accessed pages

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