javascript - consolidate multiple null/undefined checks My brain is fried from looking from looking at this for so long.  Is there a more efficient way to do this code block.  We have the ability to use es6 code too if that helps.  

First it tries to set quantity to itemTemplateIDQty.value if itemTemplateQty is not valid and itemTemplateIDQty is valid.  Then it basically checks the opposite and sets quantity to itemTemplateQty.value.  I'm thinking since in that else block, I am simply checking the exact opposite, I shouldn't need to retype all that code again.  Also I'm wondering if I can remove a lot of code by using something like quantity = itemTemplateIDQty.value || 1;.

Update
I'll try to sum up the code more clearly.  The default value for quantity should be 1 if all the checks fail.  First if there are no classes of itemTemplateQty found and 1+ classes of 'itemTemplateQty' + itemId found then set quantity to the value of the first class found of 'itemTemplateQty' + itemId.  If that check fails, then check if no classes of 'itemTemplateQty' + itemId are found and 1+ classes of itemTemplateQty are found, set quantity to the value of the first class of itemTemplateQty.



let itemId = 5;
let isDynamicRecommendation = false;
let itemTemplateQty =
        document.getElementsByClassName('itemTemplateQty').length &gt; 0 ? document.getElementsByClassName('itemTemplateQty') : null;
    let itemTemplateIDQty =
        document.getElementsByClassName('itemTemplateQty' + itemId).length &gt; 0 ? document.getElementsByClassName('itemTemplateQty' + itemId) : null;
    let quantity = 1;
    if (!isDynamicRecommendation) {
      if ((itemTemplateQty === null || itemTemplateQty.value == null || Number.parseInt(itemTemplateQty.value) &lt;= 0) &amp;&amp;
         (itemTemplateIDQty !== null &amp;&amp; itemTemplateIDQty.value !== undefined &amp;&amp; itemTemplateIDQty.value !== null &amp;&amp; Number.parseInt(itemTemplateIDQty.value) &gt; 0)) {
           quantity = itemTemplateIDQty.value;
      } else if ((itemTemplateIDQty === null || itemTemplateIDQty.value == null || Number.parseInt(itemTemplateIDQty.value) &lt;= 0) &amp;&amp;
        (itemTemplateQty !== null &amp;&amp; itemTemplateQty.value !== undefined &amp;&amp; itemTemplateQty.value !== null &amp;&amp; Number.parseInt(itemTemplateQty.value) &gt; 0)) {
          quantity = itemTemplateQty.value;
      }
    }
console.log("quantity: ", quantity);
.getElementsByClassName() returns an HTMLCollection, not a single element. You can use bracket notation [n] to get an element at index n of the collection returned.

One if statement can be used to evalaute !itemData.isDynamicRecommendation. One if..else..if can be used to evaluate itemTemplateQty[0] &amp;&amp; itemTemplateQty[0].value &lt;= 0 or itemTemplateIDQty[0] &amp;&amp; itemTemplateIDQty[0].value &lt;= 0.

To convert the string .value to an integer you can use + operator preceding +itemTemplateQty[0].value.value or alternatively use .valueAsNumber.



let itemData = 5;
let isDynamicRecommendation = false;

let itemTemplateQty = document.getElementsByClassName('itemTemplateQty')[0];
let itemTemplateIDQty = document.getElementsByClassName('itemTemplateQty' + itemData)[0];
let quantity = 1;
if (!isDynamicRecommendation) {
  if (itemTemplateQty &amp;&amp; itemTemplateQty.value &lt;= 0) {
    quantity = +itemTemplateQty.value;
  } else if (itemTemplateIDQty &amp;&amp; itemTemplateIDQty.value &lt;= 0) {
    quantity = +itemTemplateIDQty.value;
  }
}

console.log({quantity});