title: JavaScript recursive function using ternary operator
body: <p>why is it necessary to add return statement before ternary operator in recursive function to return function output?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// This dose not work
  
      function rec(n) {
        n == 1 ? n : n + rec(n - 1);
      }
  
  // This works as return statement is added before ternary operator
      function rec(n) {
        return n == 1 ? n : n + rec(n - 1);
      }
  
  // This works
      function rec(n) {
        if (n == 1) return 1;
        return n + rec(n - 1);
      }</code></pre>
  </div>
  </div>
  </p>