var foo = false; if(!foo) { // will log console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will not log as foo is defined console.log("foo is undefined") }
another example without defining foo
if(!foo) { // will throw exception "Uncaught ReferenceError: foo is not defined " console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will log console.log("foo is undefined") }
angular is so effective. isUndefined (foo) does nothing but evaluate
if(typeof foo == "undefined")
wrapped to save 1 character yes.
while! -operator checks if the given variable matches false therefore
if(!foo)
coincides with
if( foo != true)
UPDATE:
As stated in the comments, when I write "evaluates to false", there is false null undefined NaN "" (empty string) and 0 are included
source share