javascript - consolidate multiple null/undefined checks
.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});