I am creating a project using angularjs.I have variable like
$scope.test = null
$scope.test = undefined
$scope.test = ""
I want to check all null,undefined and empty value in one condition
just use -
if(!a) // if a is negative,undefined,null,empty value then...
{
// do whatever
}
else {
// do whatever
}
this works because of the == difference from === in javascript, which converts some values to "equal" values in other types to check for equality, as opposed for === which simply checks if the values equal. so basically the == operator know to convert the "", null, undefined to a false value. which is exactly what you need.
$scope.$watch() for example, you want to check if the new value is undefined or null, but if that value is a boolean, using your solution will not work.You can use angular's function called angular.isUndefined(value) returns boolean.
You may read more about angular's functions here: AngularJS Functions (isUndefined)
A very simple check that you can do:
Explanation 1:
if (value) {
// it will come inside
// If value is either undefined, null or ''(empty string)
}
Explanation 2:
(!value) ? "Case 1" : "Case 2"
If the value is either undefined , null or '' then Case 1 otherwise for any other value of value Case 2.
You can also do a simple check using function,
$scope.isNullOrEmptyOrUndefined = function (value) {
return !value;
}
value might be 0.