0

I am fairly new to JS and I realize that the following might be some sort of shorthand notation, but I have not seen something like this before and therefore am not sure what this statement is saying. Can someone please explain it to me?

if ($scope.userInformationData) {
    $scope.callReport.Created_In_S1_App__c = ($scope.userInformationData.media == 'SALESFORCE1' ? true : false);
} else {
    $scope.callReport.Created_In_S1_App__c = true;
}

Having trouble understanding this if/else block (with the ? : notation) Thanks for the help and I appreciate the explanation

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • if condition ? when_True : when_false; `var canDrink = person.Age < 18 ? false : true;` – Catalin Jul 11 '16 at 14:34
  • Your book should explain this. – Lightness Races in Orbit Jul 11 '16 at 14:35
  • Ternary operator are present in some language such as Java, C (family) etc... Here is some info on it : https://en.wikipedia.org/wiki/%3F:#JavaScript – Chax Jul 11 '16 at 14:37
  • The code you've supplied is a very poor example of its usage, to be honest, as it's trying to check a Boolean value for true or false, then set another value to true or false based on that value, which is complete redundancy. This... `$scope.callReport.Created_In_S1_App__c = ($scope.userInformationData.media == 'SALESFORCE1' ? true : false);` ... should simply be... `$scope.callReport.Created_In_S1_App__c = $scope.userInformationData.media == 'SALESFORCE1';` – ManoDestra Jul 11 '16 at 14:41

1 Answers1

0

You coud use the ternary operator ?:

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

$scope.callReport.Created_In_S1_App__c = $scope.userInformationData ?
    $scope.userInformationData.media == 'SALESFORCE1' :
    true;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392