简体   繁体   中英

How to check null and undefined both values for the property?

I have two properties where i need to check null and undefined both for each, how can i use that in if else statements ?

main.js

   var validateControlRating = function () {
            if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null || 
                $scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
                &&
               ($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
                $scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {
              $scope.caculatedRatingDiv = false;
            } else {
            $http.get('app/control/rest/calculateControlEffectiveness/' + $scope.controlProcessRatingDTO.controlDesignRatingKey + '/' + $scope.controlProcessRatingDTO.controlPerformanceRatingKey).success(function (data) {
                $scope.calcaulatedRating = data;
            }, function (error) {
                $scope.statusClass ='status invalid userErrorInfo';
                var errorMessage = error.data.errorMsg;
                if (error.data.techErrorMsg) {
                    errorMessage = error.data.techErrorMsg;
                }
                $scope.statusInfo = errorMessage;
             });
            $scope.ratingValidationMsg = '';
            $scope.ratingWinValidationClass = 'valid';
            $scope.caculatedRatingDiv = true;
            $scope.enableRatingSave = false;
        }
    };

It's a little tedious in javascript, you have to write each condition, and use parentheses etc

if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null || 
      $scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
      &&
     ($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
      $scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {...

or just

if ([null, undefined].indexOf( $scope.controlProcessRatingDTO.controlPerformanceRatingKey ) === -1
    &&
    [null, undefined].indexOf( $scope.controlProcessRatingDTO.controlDesignRatingKey ) === -1) {...

I think you need to to check this correclty, check for undefined then for null and use && not || because your code will go to check null value for undefined variable and this surely will throw exception code:

if( typeof myVar == 'undefined' ? false: myVar )
{    // go here defined and value not null  

}

or code:

if(typeof myVar != 'undefined' && myVar)
{    // go here defined and value not null  

}

In your code check will go like

if ((typeof $scope.controlProcessRatingDTO.controlDesignRatingKey !== undefined||
     typeof $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== undefined) &&
    ($scope.controlProcessRatingDTO.controlDesignRatingKey !== null ||
      $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== null)) {
    //  do home work
}else {  // do other home work  }

You can use negate operator as well, but this would make work for "false" as well:

if (!$scope.controlProcessRatingDTO.controlPerformanceRatingKey && !$scope.controlProcessRatingDTO.controlDesignRatingKey) { 

This is a bit shorter but if you want to treat False values separately, then use the Adeneo's answer above.

You could do this:

if ( some_variable == null ){
  // some_variable is either null or undefined
}

taken from: How to check for an undefined or null variable in JavaScript?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM