HTML and CSS Reference
In-Depth Information
second is always left over each day. As the days accumulate, these fractions of a second
add up. Most time devices, such as atomic clocks, account for this accumulation by
adding a leap second on certain days of the year. The effect of adding these leap seconds
is included in any time calculations you make with JavaScript. As you can see, more
is going on in calculating the time difference between one date and another than may
appear at first glance.
Controlling How JavaScript Works with
Numeric Values
As you perform mathematical calculations using JavaScript, you'll encounter situations
in which you need to work with the properties of numeric values themselves. JavaScript
provides several methods that allow you to examine the properties of numbers and
specify how they're displayed on a Web page.
Handling Illegal Operations
Some mathematical operations can return results that are not numeric values. For
example, you cannot divide a number by a text string. If you attempted to perform the
operation
var x = 5/”A”;
document.write(x);
in a script, the Web page would display the text string NaN , which stands for not a
number . This is JavaScript's way to indicate that you are attempting an operation that
should involve a numeric value, but doesn't. You can check for the presence of this par-
ticular error using the isNaN() function
isNaN( value )
where value is the value or variable you want to test for being numeric. The isNaN()
function returns a Boolean value: true if the value is not numeric, and false otherwise.
The use of the isNaN() function is one way to locate illegal operations in code in which
non-numeric values are treated as numeric.
Another illegal operation is attempting to divide a number by 0, as in the following code:
var x = 5/0;
document.write(x);
This code results in the value Infinity being written to the Web page. The Infinity
value indicates that you've attempted a numeric calculation whose result is greater than
the largest numeric value supported by JavaScript. An Infinity value is also generated
for operations whose result is less than the smallest numeric value. JavaScript is limited
to numeric values that fall between approximately 1.8 × 10 -308 and 1.8 × 10 308 . Any
operation that exceeds those bounds, such as dividing a number by 0, causes JavaScript
to assign a value of Infinity to the result. You can check for this outcome using the
function
isFinite( value )
Search WWH ::

Custom Search