title: TypeScript Recursive function returns undefined
body: <p>I have an array of Objects with the following structure:</p>
  
  <pre><code>{
    Name: "Automotive"
    RefCategory: 1,
    ChildCategories:[{
       Name: "Car"
       RefCategory: 2,
       ChildCategories: []
    },{
       Name: "Motorcycle"
       RefCategory: 3,
       ChildCategories: []
    }]
  }
  </code></pre>
  
  <p>I wrote a function to find a Category Node given its Ref:</p>
  
  <pre><code>navigateToNode(node: any, RefCategoryToFind: number): any {
  
      if (node.RefCategory == RefCategoryToFind)
          return node;
  
      node.ChildCategories.forEach(value =&gt; {
          if (value.RefCategory == RefCategoryToFind)
              return value;
          else {
              if (value.ChildCategories.length !== 0)
                  return this.navigateToNode(value, RefCategoryToFind);                
          }
      });
  }
  </code></pre>
  
  <p>This function returns null when I call</p>
  
  <pre><code>let x = this.navigateToNode(this.activeCategories[0], 2);
  </code></pre>
  
  <p>I can see it hits the <code>return value;</code> and thus found the right node, but returns undefined.</p>