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 will check if variable of indeterminate nesting is undefined
function Undef(str)
{
var ary = str.split('.');
var w = window;
for (i in ary) {
try { if (typeof(w = w[ary[i]]) === "undefined") return true; }
catch(e) { return true; }
}
return false;
}
if (!Undef("google.translate.TranslateElement")) {
The above checks if the Google translate function TranslateElement exists. This is equivalent to:
if (!(typeof google === "undefined"
|| typeof google.translate === "undefined"
|| typeof google.translate.TranslateElement === "undefined")) {