Javascript: avoid exceptions for string to integer or float typecast conversion with null or undefined variables?

Problem

You want to reliably handle type cast conversion between float, integer, and string types reliably in cases where the variables may not all be declared or may otherwise potentially cause a Javascript exception.

Solution

If at all possible, make sure all variables are at least declared before attempting a type cast conversion.

Also, understand how to handle nulls and understand equality testing in Javascript.

Details

One easy way to do robust type-checking in this Javascript scenario is to avoid:

  • undeclared variables
  • undeclared properties on Javascript objects (aka dictionary)
  • null values
  • NaN values

Here is a simple and quick overview:

//
var vfftest       = 0.05;                       // float
var viitest       = 3000;                       // integer
var vssblank      = '';                         // empty string
var vssnonblank   = 'hello';                    // non-empty string
var vddempty      = {};                         // dictionary with no name-value pairs
var vddnonempty   = {'alpha':1,'bravo':'two'};  // dictionary with name-value pairs
var vnull         = null;                       // null

// check boolean
console.log( (vssnonblank) ? 'true' : 'false' ); // true
console.log( (vssblank)    ? 'true' : 'false' ); // false
console.log( (vfftest)     ? 'true' : 'false' ); // true
console.log( (viitest)     ? 'true' : 'false' ); // true
console.log( (vnull)       ? 'true' : 'false' ); // false
console.log( (vddempty)    ? 'true' : 'false' ); // true
console.log( (vddnonempty) ? 'true' : 'false' ); // true
console.log( (vnoExisto)   ? 'true' : 'false' ); // EXCEPTION

// check toString
console.log( (vssnonblank).toString()  );  // hello
console.log( (vssblank).toString()     );  //
console.log( (vfftest).toString()      );  // '0.05'
console.log( (viitest).toString()      );  // '3000'
console.log( (vnull).toString()        );  // EXCEPTION
console.log( (vddempty).toString()     );  // [object Object]
console.log( (vddnonempty).toString()  );  // [object Object]
console.log( (vnoExisto).toString()    );  // EXCEPTION

// check parseFloat
console.log( parseFloat(vssnonblank) ); // NaN
console.log( parseFloat(vssblank) );    // NaN
console.log( parseFloat(vfftest) );     // 0.05
console.log( parseFloat(viitest) );     // 3000
console.log( parseFloat(vnull) );       // NaN
console.log( parseFloat(vddempty) );    // NaN
console.log( parseFloat(vddnonempty) ); // NaN
console.log( parseFloat(vnoExisto) );   // EXCEPTION

// check parseInt
console.log( parseInt(vssnonblank) );  // NaN
console.log( parseInt(vssblank) );     // NaN
console.log( parseInt(vfftest) );      // 0
console.log( parseInt(viitest) );      // 3000
console.log( parseInt(vnull) );        // NaN
console.log( parseInt(vddempty) );     // NaN
console.log( parseInt(vddnonempty) );  // NaN
console.log( parseInt(vnoExisto) );    // EXCEPTION

// check typeof
console.log(typeof vssnonblank);       // string
console.log(typeof vssblank);          // string
console.log(typeof vfftest);           // number
console.log(typeof viitest);           // number
console.log(typeof vddempty );         // object
console.log(typeof vddnonempty );      // object
console.log(typeof vnull);             // object
console.log(typeof vnoExisto);         // 'undefined'

Pitfalls

  • <undeclared> throws an exception for parseInt parseFloat and .toString()
  • null.toString() throws an exception
  • parseInt(null) and parseFloat(null) returns NaN
  • If you can, at least make sure all variables are declared. This will prevent exceptions for undeclared values, but not for nulls.
  • Even if you use a try-catch block, and make sure all variables are declared, you still will have to handle exceptions for nulls, and these will potentially halt your code.

See also

The following links provide additional details relevant to type-cast and comparison in Javascript:

Leave a Comment