1. About.com
  2. Computing & Technology
  3. JavaScript

JavaScript By Example

Functions: 4. isFinite

From , former About.com Guide

While JavaScript can cover a really big range of numbers by using floating point there are still limits. Numbers that become too small to be able to express are converted to zero since theyt are so close to that as to make no difference. JavaScript provides Infinity as the value that will be used if the number is too big to be expressed as a floating point value. The largest value JavaScript can express using floating point is 1.7976931348623157E+10308 so anything bigger than that will be Infinity.

We can't actually perform any calculations using Infinity even though it is a number and so JavaScript provides the isFinite function to allow us to test if the number is Infinity prior to trying ti use it in a calculation.

This function tests both that the value is a number and that the number isn't Infinity or -Infinity. It returns true if the value is a finite number and so can make for easier to read code than using isNaN where you know the number isn't going to be Infinity.


var googol, str;
str = '+1.0e+100';
if (isFinite(str))
  googol = parseFloat(str);

Reader Submissions: Help Others By Providing Your Own JavaScript Examples

JavaScript By Example

©2012 About.com. All rights reserved. 

A part of The New York Times Company.