title: JS - See which "or" evaluated true in if statement
body: <p>I have the following JS (with jQuery):</p>
  
  <pre><code>if ($('#username').val() === '' || $('#password').val() === '') {
    return;
  }
  $.ajax(); //run an ajax call to the server for password verif (this would have proper ajax code).
  </code></pre>
  
  <p>What this means is: if the username (<code>#username</code>) or password (<code>#password</code>) field are blank, then return.</p>
  
  <p>I want to give the user a message if this is the case. Something like:</p>
  
  <pre><code>if ($('#username').val() === '' || $('#password').val() === '') {
    if ($('#username').val() === '' &amp;&amp; $('#password').val() !== '') {
      alert('Please enter a username');
      return;
    }
    if ($('#username').val() !== '' &amp;&amp; $('#password').val() === '') {
      alert('Please enter a password');
      return;
    }
    alert('Username and password fields cannot be blank');
  }
  </code></pre>
  
  <p>However, that is a lot of code. I am looking for something like an <code>XOR</code>/<code>^</code> <a href="https://stackoverflow.com/questions/3769200/what-does-a-caret-do-in-a-sql-query">operator</a> (from SQL), but for JS. Or something which says, "Good! The first expression was true and the second one was false - he didn't enter in a username but entered in a password. Let's tell him to enter in a username. Or - he didn't enter in a password? Tell him to enter in a password. And finally - he didn't enter in anything? Well tell him to enter in a username and password.</p>
  
  <blockquote>
    <p>I am aware that the current code I have would do that, but I am looking for a simpler, more <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow noreferrer">DRY</a> conformant code.</p>
  </blockquote>
  
  <p>My research on google has not come up with any solutions, and thus I am asking stackoverflow.</p>
  
  <p>Thank you.</p>