AngularJS - What is the best way to detect undefined zero or empty value at the same time?

Sometimes I need to check the value for three conditions at the same time, null, undefined or "". Due to the fact that I did not find any way to do this, I encoded my own and it works.

$scope.isNullOrEmptyOrUndefined = function (value) { if (value === "" || value === null || typeof value === "undefined") { return true; } } 

Just wanted to know if there is a better way to do the same.

Thank you very much in advance,

Guillermo

+6
source share
4 answers

Update

As mentioned in the comments, it is better to use return !value .

 $scope.isValid = function(value) { return !value } 

Old and incomplete answer

The correct way is to simply use angular.isDefined()

+4
source

How about this? Since you seem to return true in those cases null / undefined:

 $scope.isNullOrEmptyOrUndefined = function (value) { return !value; } 

http://jsfiddle.net/1feLd9yn/3/

Note that an empty array and an empty object also return false, since they are right values. If you want true / false returns to be upside down, omit! before the value.

+12
source
 **AngularJS - What is the best way to detect undefined null or empty value at the same time?** 

You can define a function or use the built-in command.

 $scope.isNullOrUndefined = function(value){ return (!value) && (value === null) } or var temp = null; (temp) ? 'I am not null' : 'yes I am null or undefined'; "yes I am null or undefined" var temp = undefined (temp) ? 'I am not null' : 'yes I am null or undefined'; "yes I am null or undefined" temp = '123123Rec' (temp) ? 'I am not null' : 'yes I am null or undefined'; "I am not null" 
+1
source

I tried the code below, it works fine in my angular js application.

 function isValid(value) { return typeof value !== 'undefined'; }; 
0
source

Source: https://habr.com/ru/post/976798/


All Articles