Javascript: avoid exceptions for string to integer or float typecast conversion with null or undefined variables? -
background javascript (v8 engine) google chrome browser inherited javascript code lots of type casting of numeric variables. question how reliably type-cast javascript string numeric variables (float , integer) , still avoid script halting exceptions due undeclared or null values? details
tymac has inherited javascript code requires lot of type casting of variables float integer string , numerous permutations between these 3 types.
the problem is, variables declared or defined irregularly, introduces potential unpredictably. also, code arranged in such way create hard sort out.
the goal come 'risk proof' way type-cast variables between float-integer-string when variable declarations not known in advance because of way code set up.
problem
you want reliably handle type cast conversion between float, integer, , string types reliably in cases variables may not declared or may otherwise potentially cause javascript exception.
solutionif @ possible, create sure variables @ to the lowest degree declared before attempting type cast conversion.
also, understand how handle nulls , understand equality testing in javascript.
detailsone easy way robust type-checking in javascript scenario avoid:
undeclared variables undeclared properties on javascript objects (aka dictionary) null values nan valueshere simple , quick overview:
// var vfftest = 0.05; // float var viitest = 3000; // integer var vssblank = ''; // empty string var vssnonblank = 'hello'; // non-empty string var vddempty = {}; // dictionary no name-value pairs var vddnonempty = {'alpha':1,'bravo':'two'}; // dictionary 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 exception parseint parsefloat , .tostring()
null.tostring()
throws exception parseint(null) , parsefloat(null) returns nan
if can, @ to the lowest degree create sure variables declared. prevent exceptions undeclared values, not nulls. even if utilize try-catch block, , create sure variables declared, still have handle exceptions nulls, , these potentially halt code. see also the next links provide additional details relevant type-cast , comparing in javascript:
checking null before tostring() what's reliable way check if javascript variable null? does matter equals operator (== vs ===) utilize in javascript comparisons? how convert string float in javascript? javascript type casting do not stop javascript when throws exception finding variable type in javascript javascript type-conversion
No comments:
Post a Comment