title: Javascript search an array for a value and get its key
body: <p>I want to make a function that works like this:</p>
  
  <pre><code>function arraySearch(array, valuetosearchfor)
  {
  // some code
  }
  </code></pre>
  
  <p>if it finds the value in the array, it will return the key, where it found the value. If there is more than one result (more than one key), or no results at all (nothing found), then the function will return FALSE.</p>
  
  <p>I found this code:</p>
  
  <pre><code>function arraySearch(arr,val) 
          {
              for (var i=0; i&lt;arr.length; i++)
                  {
                      if (arr[i] == val) 
                      {                    
                          return i;
                      }
                      else 
                      {
                          return false;
                      }
                  }
          }
  </code></pre>
  
  <p>and used it like this:</p>
  
  <pre><code>var resultofarraycheck = arraySearch(board, chosen);
                  if (resultofarraycheck === false) 
                  {
                         document.getElementById(buttonid).value;
                         chosen = 0;
                  }
  </code></pre>
  
  <p>But it doesn't seem to work.
  When it should find something, it returns false instead of the key (i).</p>
  
  <p>How can I fix this, or what am I doing wrong?</p>
  
  <p>Thanks, and I'm sorry if my English wasn't clear enough.</p>