title: JavaScript - How do I extract string in If/Else statement
body: <p>I'm trying to use the If/Else statement to write certain text on the screen after searching through the imgNumber variable. For some reason, it only writes the string 'cockroaches' no matter which text is contained in the imgNumber variable. </p>
  
  <p>Also, the imgNumber is a random string (it will always be one of the strings in the myImages variable). </p>
  
  <p>What am I missing here?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  function changeImg(imgNumber) {
  var myImages = ["images/homepage/homepage-header.jpg", "images/homepage/homepage-header-3.jpg", "images/homepage/homepage-header-5.jpg"];
  var imgShown = document.body.style.backgroundImage;
  var newImgNumber =Math.floor(Math.random()*myImages.length);
  document.body.style.backgroundImage = 'url('+myImages[newImgNumber]+')';
  
  var imgNumber = myImages[newImgNumber];
  
  if (imgNumber = "images/homepage/homepage-header-5.jpg")
    {
    document.write('cockroaches');
    }
  else if (imgNumber = "images/homepage/homepage-header-3.jpg")
    {
    document.write('ants');
    }
  else
    {
    document.write('termites');
    } 
  
  var n=myImages.indexOf("images/homepage/homepage-header-5.jpg");
  document.write(n);
  
  }
  window.onload=changeImg;
  &lt;/script&gt;
  </code></pre>