if(typeof hello! = 'undefined'){
}
This is too hard to type, would like to use underscore
According to the official documentation, you can use _.isUndefined():
_.isUndefined(window.missingVariable);
=> true
_.isUndefined(hello) => Uncaught ReferenceError: hello is not defined(…) (Tested in Chrome 52)var a; _.isUndefined(a); Attempting to access an undeclared variable will always throw a ReferenceError unless checked via typeof a === "undefined". However, this question was about how to check for undefined via Underscore.js.
hello === undefinedshould work well, even without underscore .. but in many cases I find - and design code such that - a simple "false-y" conditional is correct:if (!hello) ..The only reason to usetypeofhere is 1) for global variables, which may not exist (ick, preferwindow.propto avoid ReferenceErrors!) or 2) over paranoia aboutundefinedbeing "redefined".