1

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;
        }
    };
6
  • if (!$scope.controlProcessRatingDTO.controlPerformanceRatingKey && !$scope.controlProcessRatingDTO.controlDesignRatingKey) { Commented Jan 11, 2016 at 20:02
  • You can also use not operator as above, or like @adeneo's answer, both should work. Commented Jan 11, 2016 at 20:04
  • I've also stored the value in a variable in these situations, if the length of those values is a concern. if (perfKey === null || perfKey === undefined), etc. Commented Jan 11, 2016 at 20:08
  • @AhmetCetin not operator is not checking for null Commented Jan 11, 2016 at 20:38
  • sure it does, check here: jsbin.com/yubizakebe/edit?js,console Commented Jan 11, 2016 at 20:42

4 Answers 4

2

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) {...
Sign up to request clarification or add additional context in comments.

2 Comments

its not checking for null and executing else condition
i edited my question and added according to your answer its still thrwing error i am not sure where my code is wrong
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  }

Comments

0

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.

Comments

0

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.