Check Null,empty Or Undefined Angularjs


Answer :

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.


You can do

if($scope.test == null || $scope.test === ""){   // null == undefined } 

if false, 0 and NaN can also be considered as false values you can just do

if($scope.test){  //not any of the above } 

if($scope.test == null || $scope.test == undefined || $scope.test == "" ||    $scope.test.lenght == 0){  console.log("test is not defined"); } else{ console.log("test is defined ",$scope.test);  } 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?