Reputation: 339
I've tried:
if(angular.isUndefined(value)){
// something
}
and
if(!(value)){
// something
}
Upvotes: 6
Views: 1940
Reputation: 17906
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 define 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")
}
so effective angular.isUndefined(foo) does nothing else than evaluating
if(typeof foo == "undefined")
wrapped for saving 1 character yeah.
while !-operator checks if a defined variable evaluates to false so
if(!foo)
is the same like
if( foo != true)
UPDATE:
As stated in comments, when i write "evaluates to false" there is false
null
undefined
NaN
""
(empty string) and 0
included
Upvotes: 2
Reputation: 1837
! is the logical not operator in JavaScript while angular.isUndefined(value) checks if the reference is undefined
Which one to use completely depends on what you are trying to do in the end.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators and https://docs.angularjs.org/api/ng/function/angular.isUndefined
Upvotes: 0