Is there a universal JavaScript function that checks that a variable has a value and ensures that it\'s not undefined
or null
? I\'ve got this code,
This is the safest check and I haven't seen it posted here exactly like that:
if (typeof value !== 'undefined' && value) {
//deal with value'
};
It will cover cases where value was never defined, and also any of these:
Edited: Changed to strict equality (!==) because it's the norm by now ;)