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.
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.
One easy way to do robust type-checking in this Javascript scenario is to avoid:
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'
<undeclared> throws an exception for parseInt parseFloat and .toString()
null.toString()
throws an exceptionparseInt(null) and parseFloat(null) returns NaN
The following links provide additional details relevant to type-cast and comparison in Javascript: