0

I am trying to make a button change a string from 'Mode One:' to 'Mode Two:', and when pressed again, changed back to 'Mode One:' and so on and so forth. However when I run this, it will change the first time to 'Mode Two:' but when I press it again, it will not go back to Mode One.

Here is my code:

Flip
<script>

    let currentMode = 'Mode one:'

    flip = () => {
        if(currentMode = 'Mode one:'){
            currentMode = 'Mode Two:'
            console.log(currentMode)
        }else if(currentMode = 'Mode Two:'){
            currentMode = 'Mode one:'
            console.log(currentMode)
        }
    }

</script>
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • `=` is for **assignment**, not comparison. – Pointy Feb 11 '22 at 14:30
  • 2
    Does this answer your question? [What is the difference between the \`=\` and \`==\` operators and what is \`===\`? (Single, double, and triple equals)](https://stackoverflow.com/questions/11871616/what-is-the-difference-between-the-and-operators-and-what-is-si) Also, this useful reference: [What does this symbol mean in JavaScript?](https://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – IMSoP Feb 11 '22 at 14:31

2 Answers2

1

In your if statement, your are assigning a value to a variable. The value of this operation is always true. To compare two values you need the == operator:

<script>

    let currentMode = 'Mode one:'

    flip = () => {
        if(currentMode == 'Mode one:'){
            currentMode = 'Mode Two:'
            console.log(currentMode)
        }else if(currentMode == 'Mode Two:'){
            currentMode = 'Mode one:'
            console.log(currentMode)
        }
    }

</script>

Apollo79
  • 674
  • 3
  • 14
1

Since you are using a single "=", that is not comparing values, it is assigning a value. You want to use "==" or "===":

if(currentMode == 'Mode one:'){
   currentMode = 'Mode Two:'
   console.log(currentMode)
}else if(currentMode == 'Mode Two:'){
   currentMode = 'Mode one:'
   console.log(currentMode)
}
e-e
  • 1,071
  • 1
  • 11
  • 20