I have a number of variables in my script, each of which could contain a value, undefined or null.
I've noticed alot of repetative code checking for undefined and nulls, can a common function be created?
I have created a common fucntion to check all variables are not undefined or null:
JS Original:
var testa;
var testb;
var testc;
if ((typeof testa == "undefined" || testa == null) && (typeof testa == "undefined" || testb == null) && (typeof testc == "undefined" || testc == null)) {
//Do something ///////////
}
JS New:
var testa;
var testb;
var testc;
if (checkValues(testa) && checkValues(testb) && checkValues(testc)) {
//Do something ///////////
}
function checkValues (v) {
return (v == "undefined") || (v == null);
}
Which works. However what if testa contains a value, and testb and testc are null or undefiend? e.g:
if ((typeof testa == "undefined" || testa == null) && (typeof testa != "undefined" || testb != null) && (typeof testc != "undefined" || testc != null)) {
//Do something ///////////
}