0

Understand if the variable is valid

What kind of control do you use to understand if the variable is valid (not undefined, not null, not empty)? Something like this : hasAValue: function (value) { return (("" !== value) && (null !== value) && ("undefined" !== typeof value) ); },

19th Jan 2017, 11:31 AM
Carlo Gagliolo
Carlo Gagliolo - avatar
3 Antworten
+ 3
(value) produce already true or false, so: if (value) { }; ... is strictly equivaent to: if hasValue(value) { } The only difference being add a call to a function, a test, and context ( variables scope ), as much unnecessary time and memory requirement ^^
19th Jan 2017, 4:25 PM
visph
visph - avatar
0
Yes, but order can be improved by: - putting the typeof statement comparison at first, so if it is you don't need to perform others comparisons - condensing your strict (not) equality tests of 'null' and 'undefined' into once not strict, which is equivalent return ( (null != value) && ("" !== value) ); But you can simply test if the variable has a value which is NOT in: - null - undefined - NaN - 0 - false -"" by: return ( value ); Well, so... you don't need anymore of a function for that :P
19th Jan 2017, 11:52 AM
visph
visph - avatar
0
like this : hasAValue: function (value) { if(value){ return true; } else { return false; } }
19th Jan 2017, 2:03 PM
Carlo Gagliolo
Carlo Gagliolo - avatar