The Number and Math Objects
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
- MAX_VALUE - Returns the largest number possible in JavaScript (1.79e+308)
- MIN_VALUE - Returns the smallest number possible in JavaScript (5e-308)
- NaN - Represents the value of "Not a Number"
- NEGATIVE_INFINITY - Represents the negative infinity
- NEGATIVE_INFINITY - Represents the infinity
- prototype - Enables you to add properties to the object
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
- toExponential(x) - Returns a string value that represents the number in exponential notation.
- x is optional. An integer between 0 and 20 representing the number of digits in the notation after the decimal point.<script type="text/javascript"><!-- var num = 12; document.write(num.toExponential(2)); // 1.20e+1 --></script>
- toFixed(x) - Returns a string value that represents the number rounded to the x digits after the decimal point
<script type="text/javascript"><!-- var num = 12.34567; document.write(num.toFixed(2)); // 12.35 --></script>
- toPrecision(x) - Formats a number to x length
<script type="text/javascript"><!-- var num = 12.34567; document.write(num.toPrecision(3)); // 12.3 --></script>
- toString() - Converts a Number object to a string
<script type="text/javascript"><!-- var num = 12.34567; document.write(num.toString()); // 12.34567 --></script>
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:
- var nr = Math.random();
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).- E - Value of Euler’s constant (E), ( 2.71828...)
- LN2 - The natural logarithm of 2 ( 0.693147...)
- LN10 - Value of the natural logarithm of 10 ( 2.302585...)
- LOG2E - Value of the base 2 logarithm of E ( 1.442695...)
- LOG10E - The base 10 logarithm of E ( 0.43429...)
- PI - Value of pi, often used with circles ( 3.14159...)
- SQRT1_2 - The square root of one half ( 0.7071...)
- SQRT2 - The square root of 2 ( 1.4142...)
<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.- abs(x) - Returns the absolute value of x
- acos(x) - Returns the arccosine of x, in radians
- asin(x) - Returns the arcsine of x, in radians
- atan(x) - The arctangent of x, in radians
- atan2(y,x) - The arctangent of the quotient of two numbers sent as parameters (y,x), in radians
- ceil(x) - Returns the smallest integer greater than or equal to x
- cos(x) - The cosine of the number x, in radians
- exp(x) - Returns the value of E to the power of the x
- floor(x) - Returns x, rounded downwards to the nearest integer
- log(x) - The natural logarithm of x
- max(x1, x2, x3, ...) - Returns the number with the highest value
- min(x1, x2, x3, ...) - Returns the number with the lowest value
- pow(x,y) - The value of 'x' raised to the power of 'y'
- random() - A random number between 0 and 1
- round(x) - Returns the value of the number x rounded to the nearest integer
- sin(x) - The sine of x (x is in radians)
- sqrt(x) - Returns the square root of x
- tan(x) - The tangent of an angle x (x in radians)
- 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"><!-- document.write(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"><!-- document.write(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>
Associative Arrays in JavaScript <<-- Previous ----------- Next -->> JS Date Object