I've tried:
if(angular.isUndefined(value)){
// something
}
and
if(!(value)){
// something
}
The angular. isUndefined() function in AngularJS is used to determine the value inside isUndefined function is undefined or not. It returns true if the reference is undefined otherwise returns false. Return Value: It returns true if the value passed is undefined else returns false.
The angular. isDefined() function in AngularJS is used to determine the value inside isDefined function is defined or not. It returns true if the reference is defined otherwise returns false.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With