By using typeof
,
typeof floob.flib === 'undefined'
equals to,
floob.flib === undefined
I assume you want to check whether floob.flib
has a value, and if it does, you want to perform an operation with it.
However, in JS, there's almost simpler way to achieve this.
E.g.
if (floob.flib) {
// 'floob.flib' is NOT 'null', 'empty string', '0', false or 'undefined'
}
This also works well if you want to assign variable with ternary ( ?:
) operators.
var str = floob.flib ? 'exists' : 'does not exist';
Or even using logical OR ( ||
)
var str = floob.flib || '<= either null, empty, false, 0 or undefined';
Note that unless floob.flib
does not produce ReferenceError
exception, the code above should work just fine.