title: Finding the next largest number in an array using JavaScript If...Else Statements
body: <p>Would you be so kind and explain how "else if" statement in the below piece of code returns the second largest number from an array?</p>
  
  <p>Especially that there is no AND operator (&amp;&amp;) within "else if statement". I mean for ex.:</p>
  
  <pre><code>else if (max2 &lt; array[i] &amp;&amp; max1 &gt; array[i])
  </code></pre>
  
  <p>I found people using this kind of approach on stackOverflow but they did not give any detailed explanations ("why it works"): </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>var arr1 = [2, 3, 1, 6, 100, 49, 5, 7, 8, 9];
  
  function getSecondMaxNumber(array) {
      var max1 = 0;
      var max2 = 0;
      for (var i = 0; i &lt;= array.length; i++) {
          if (max1 &lt; array[i]) {
              max1 = array[i];
          } else if (max2 &lt; array[i]) {
              max2 = array[i];
          }
      }
      return max2;
  }
  console.log(getSecondMaxNumber(arr1));</code></pre>
  </div>
  </div>
  </p>
  
  <p>UPDATE</p>
  
  <p>The above method is wrong as Hassan commented below.</p>
  
  <p>I decided to use:</p>
  
  <pre><code>var arr1 = [2, 3, 1, 6, 100, 49, 5, 7, 8, 9];
  
  
  function getSecondMaxNumber(array) {
      function sortNumbers(a, b) { //Compare numbers.
          return a - b;
      }
      var arrSorted = array.sort(sortNumbers); //Set up sorting.
      return arrSorted[arrSorted.length - 2]; //Choose the second largest number in an array.
  }
  console.log(getSecondMaxNumber(arr1)); //Show me that number.
  </code></pre>