title: How to give a function a return value when the return is inside an if statement
body: <p>I had a function that was initially like this, and worked:</p>
  
  <pre><code>  productGroupPages(): PDF.Page[] {
      const descriptions = {};
      this.areas.forEach(area =&gt; descriptions[area.id] = area.description);
      return this.json.engagementAreas.map(area =&gt; {
        return this.productGroupPage(area, descriptions[area.id]);
      });
    }
  </code></pre>
  
  <p>Basically everything isnt wrapped in the for each.</p>
  
  <p>I had to alter to the function as i wanted to go through a for each, to then use an if statement so i would only return values that actually contained a certain value, the result is that my return statement is too early, I cant move it to the end because of the for loop and im struggling to find out how I can get past this,</p>
  
  <p>this new function:</p>
  
  <pre><code>  productGroupPages(): PDF.Page[] {
      const descriptions = {};
      let pages = {};
      console.log('this .json' + JSON.stringify(this.json))
      this.json.engagementAreas.forEach(area =&gt; {
        descriptions[area.id] = area.description
  
        if (area.engagementTypes.length !== 0) {
  
          return this.json.engagementAreas.map(area =&gt; {
            return this.productGroupPage(area, descriptions[area.id]);
          });
        }
      })
    }
  </code></pre>
  
  <p>I tried creating a variable, an array or object and equaling that to the return value and then returning that near the end of the scope but this wouldnt let me it said the return type was wrong.</p>
  
  <p>Any help would be greatly appreciated.</p>