Why doesn't the if statement work?

  • 0
    Good evening. There are selects and they must change some values ​​depending on the option selected at the moment, but for some reason the construction through if fulfills only 1 condition and that's all, but through the switch everything works as it should.
    if(e.target.value = 'amd') {
        ads.textContent = amd
     } else if(e.target.value = 'azn') {
         ads.textContent = azn
      } else {
         ads.textContent = usd
      }

    where is the mistake? Should work the same as with
    switch(e.target.value) {
       case 'amd':
          ads.textContent = amd
          break;
       case 'azn':
          ads.textContent = azn
          break;
        case 'usd':
          ads.textContent = usd
          break;
     }
    JavaScript Phoebe Vincent, Jan 17, 2020

  • 2 Answers
  • 0
    if(e.target.value === 'amd') { // '=' => '==='
    ads.textContent = amd
    } else if(e.target.value === 'azn') { // '=' => '==='
    ads.textContent = azn
    } else {
    ads.textContent = usd
    }




    Should work the same as with




    No, it will work differently. else will intercept everything except amd, azn.

    Switch will only work on 3 variants.
    Dominic Anthony

  • 0
    if (e.target.value = 'amd')

    In ifs, you have a spelling mistake, instead of a comparison, you make an assignment. Instead of one "=" sign, there should be "=="
    Silas Reynolds

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!