NEGATIVE_INFINITY
This is a special
numeric value representing a value less than Number.MIN_VALUE. This value is
represented as "-Infinity". It resembles an infinity in its
mathematical behavior. For example, anything multiplied by NEGATIVE_INFINITY is
NEGATIVE_INFINITY, and anything divided by NEGATIVE_INFINITY is zero.
Syntax
The syntax to use NEGATIVE_INFINITY is as follows:
var val = Number. NEGATIVE_INFINITY;
Example
Try the following example.
<html>
<head>
<script
type="text/javascript">
<!--
function showValue()
{
var smallNumber = (-Number.MAX_VALUE) * 2
if (smallNumber ==
Number.NEGATIVE_INFINITY) { alert("Value of smallNumber : " +
smallNumber );
}
}
//-->
</script>
</head>
<body>
<p>Click the following to see
the result:</p> <form>
<input type="button"
value="Click Me" onclick="showValue();" />
</form>
</body>
</html>
Output
Click the following to see the result:
Click Me
Value of val : -Infinity
POSITIVE_INFINITY
This is a special
numeric value representing any value greater than Number.MAX_VALUE. This value
is represented as "Infinity". It resembles an infinity in its
mathematical behavior. For example, anything multiplied by POSITIVE_INFINITY is
POSITIVE_INFINITY, and anything divided by POSITIVE_INFINITY is zero.
As POSITIVE_INFINITY is a constant, it is a read-only property of
Number.
Syntax
Use the following syntax to use POSITIVE_INFINITY.
var val = Number. POSITIVE_INFINITY;
Example
Try the following example to learn how use POSITIVE_INFINITY.
<html>
<head>
<script
type="text/javascript">
<!--
function showValue()
{
var bigNumber = Number.MAX_VALUE * 2
if (bigNumber == Number.POSITIVE_INFINITY) {
alert("Value of bigNumber : " +
bigNumber );
}
}
//-->
</script>
</head>
<body>
<p>Click the following to see
the result:</p> <form>
<input type="button"
value="Click Me" onclick="showValue();" />
</form>
</body>
</html>
Output
Click the following to see the result:
Click Me
Value of val : Infinity
Prototype
The prototype property allows you to add
properties and methods to any object (Number, Boolean, String and Date etc.).
Note: Prototype is a global
property which is available with almost all the objects.
Syntax
Use the following syntax to use Prototype.
object.prototype.name = value
Example
Try the following
example to use the prototype property to add a property to an object.
<html>
<head>
<title>User-defined
objects</title>
<script type="text/javascript">
function book(title, author){
this.title = title;
this.author = author;
}
</script>
</head>
<body>
<script
type="text/javascript">
var myBook = new
book("Perl", "Mohtashim"); book.prototype.price = null;
myBook.price = 100;
document.write("Book title is : " +
myBook.title + "<br>");
document.write("Book author is : "
+ myBook.author + "<br>");
document.write("Book price is : " +
myBook.price + "<br>");
</script>
</body>
</html>
Output
Book title is : Perl
Book author is : Mohtashim
Book price is : 100
constructor
It returns a reference to the Number function
that created the instance's prototype.
Syntax
Its syntax is as follows:
number.constructor()
Return value
Returns the function that created this object's instance.
Example
Try the following example.
<html>
<head>
<title>JavaScript constructor()
Method</title>
</head>
<body>
<script
type="text/javascript">
var num = new Number( 177.1234 );
document.write("num.constructor() is : " + num.constructor);
</script>
</body>
</html>
Output
num.constructor() is : function Number() {
[native code] }
Number Methods
The Number object contains only the default
methods that are a part of every object's definition.
Method
|
Description
|
|
toExponential() Forces a number to display in exponential notation, even if
the number is in the
range in which JavaScript normally uses standard notation.
toFixed() Formats a number with a
specific number of digits to the right of the decimal.
toLocaleString() Returns a string value version of the current
number in a format that may vary according to a browser's local settings.
toPrecision()
|
Defines how many total digits (including
digits to the left and
|
right of the decimal) to display of a
number.
|
|
toString()
|
Returns the string representation of the
number's value.
|
valueOf()
|
Returns the number's value.
|
In the following
sections, we will have a few examples to explain the methods of Number.
toExponential ()
This method returns a
string representing the number
object in exponential notation.
Syntax
Its syntax is as follows:
number.toExponential( [fractionDigits] )
Parameter Details
fractionDigits: An integer specifying the number of digits after the decimal point. Defaults to as many digits as
necessary to specify the number.
Return Value
A string representing a
Number object in exponential notation with one digit before the decimal point,
rounded to fractionDigits digits
after the decimal point. If the fractionDigits
argument is omitted, the number of digits after the decimal point defaults to
the number of digits necessary to represent the value uniquely.
Example
Try the following example.
<html>
<head>
<title>Javascript Method
toExponential()</title>
</head>
<body>
<script
type="text/javascript">
var num=77.1234;
var val = num.toExponential();
document.write("num.toExponential() is : " + val );
document.write("<br />");
val = num.toExponential(4);
document.write("num.toExponential(4) is : " + val );
document.write("<br />");
val = num.toExponential(2);
document.write("num.toExponential(2) is : " + val);
document.write("<br />");
val = 77.1234.toExponential();
document.write("77.1234.toExponential()is : " + val );
document.write("<br />");
val = 77.1234.toExponential();
document.write("77 .toExponential() is :
" + val);
</script>
</body>
</html>
Output
num.toExponential() is : 7.71234e+1
num.toExponential(4) is : 7.7123e+1
num.toExponential(2) is : 7.71e+1
77.1234.toExponential()is : 7.71234e+1
77 .toExponential() is : 7.71234e+1
toFixed ()
This method formats a number with a specific number of digits to the right of the
decimal.
Syntax
Its syntax is as follows:
number.toFixed( [digits] )
Parameter Details
digits: The number of digits to appear after the
decimal point.
Return Value
A string representation of number that does not use exponential
notation and has the exact number of digits
after the decimal place.
Example
Try the following example.
<html>
<head>
<title>JavaScript toFixed()
Method</title>
</head>
<body>
<script
type="text/javascript">
var num=177.1234;
document.write("num.toFixed() is :
" + num.toFixed());
document.write("<br />");
document.write("num.toFixed(6) is :
" + num.toFixed(6));
document.write("<br />");
document.write("num.toFixed(1) is :
" + num.toFixed(1));
document.write("<br />");
document.write("(1.23e+20).toFixed(2)
is:" +
(1.23e+20).toFixed(2));
document.write("<br
/>"); document.write("(1.23e-10).toFixed(2) is : " +
(1.23e-10).toFixed(2));
</script>
</body>
</html>
Output
num.toFixed() is : 177
num.toFixed(6) is : 177.123400
num.toFixed(1) is : 177.1
(1.23e+20).toFixed(2)
is:123000000000000000000.00
(1.23e-10).toFixed(2) is : 0.00
toLocaleString ()
This method converts a number object into a human readable string representing the number
using the locale of the environment.
Syntax
Its syntax is as follows:
number.toLocaleString()
Return Value
Returns a human readable string representing
the number using the locale of the environment.
Example
Try the following example.
<html>
<head>
<title>JavaScript toLocaleString()
Method </title>
</head>
<body>
<script
type="text/javascript">
var num = new Number(177.1234);
document.write( num.toLocaleString());
</script>
</body>
</html>
Output
177.123
toPrecision ()
This method returns a string representing the number object to the specified
precision.
Syntax
Its syntax is as follows:
number.toPrecision( [ precision ] )
Parameter Details
precision: An integer specifying the number of
significant digits.
Return Value
Returns a string representing a Number object
in fixed-point or exponential notation rounded toprecision significant digits.
Example
Try the following example.
<html>
<head>
<title>JavaScript toPrecision() Method
</title>
</head>
<body>
<script
type="text/javascript">
var num = new Number(7.123456);
document.write("num.toPrecision()
is " + num.toPrecision()); document.write("<br />");
document.write("num.toPrecision(4)
is " + num.toPrecision(4)); document.write("<br />");
document.write("num.toPrecision(2)
is " + num.toPrecision(2)); document.write("<br />");
document.write("num.toPrecision(1) is
" + num.toPrecision(1));
</script>
</body>
</html>
Output
num.toPrecision() is 7.123456
num.toPrecision(4) is 7.123
num.toPrecision(2) is 7.1
num.toPrecision(1) is 7
toString ()
This method returns a
string representing the specified object. The toString() method parses its first argument, and attempts to return
a string representation in the specified radix (base).
Syntax
Its syntax is as follows:
number.toString( [radix] )
Parameter Details
radix: An integer between 2 and 36 specifying the base to use for
representing numeric values.
Return Value
Returns a string representing the specified Number object.
Example
Try the following example.
<html>
<head>
<title>JavaScript toString() Method
</title>
</head>
<body>
<script
type="text/javascript">
var num = new Number(15);
document.write("num.toString() is "
+ num.toString());
document.write("<br />");
document.write("num.toString(2)
is " + num.toString(2)); document.write("<br />");
document.write("num.toString(4)
is " + num.toString(4)); document.write("<br />");
</script>
</body>
</html>
Output
num.toString() is 15
num.toString(2) is 1111
num.toString(4) is 33
valueOf ()
This method returns the primitive value of the specified number object.
Syntax
Its syntax is as follows:
number.valueOf()
Return Value
Returns the primitive value of the specified number object.
Example
Try the following example.
<html>
<head>
<title>JavaScript valueOf() Method
</title>
</head>
<body>
<script
type="text/javascript"> var num = new Number(15.11234);
document.write("num.valueOf() is " + num.valueOf());
</script>
</body>
</html>
Output
num.valueOf() is 15.11234
0 comments:
Post a Comment