Introduction to JavaScript

parseInt() & parseFloat()

  • parseInt("12.34");                 // returns 12
  • parseInt("3 blind mice");      // returns 3
  • parseFloat("3.14 meters");    // returns 3.14
  • parseInt("3.14 meters");        // returns 3
  • parseInt("eleven");                // returns NaN
  • parseFloat("72.74");              // returns 72.74
  • parseFloat("$72.74");            // returns NaN

Summary: parseInt() pulls out the Integer from a string while parseFloat() pulls out the Float from a string.

NOTE: The string must start with a number otherwise NaN (Not a Number) is returned.

The code below is an example of "browser detection"

var br;
var IE = "Microsoft Internet Explorer";

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);

NOTE:
if ((bName == "Netscape" && bVer >= 3) || (bName == IE && bVer >= 4)) br = "n3";
else br = "n2";

// "n3" is short for Netscape 3.x or greater or JavaScript 1.1 or greater

if (br == "n3") {
     etc...
}

bName (browser Name) & bVer (browser Version) give us enough information to decide what the browsers are capable of based on the information we just gathered.