Skip to content Skip to sidebar Skip to footer

Javascript: Else If Condition Not Working

The button that I'm working on, when clicked, pans a div to the left. In the script, the 'if' condition works fine but the 'if else' condition doesn't. Did I forgot something to in

Solution 1:

In Javascript, the equal (=) operators work as follows:

  • x = y is assignation (i.e. give variable X value Y)
  • x == y is a loose equality test (i.e. is x equal to y), it's loose because it does type coercion (changes types to try and find a match), such as if "1" == 1 (string vs number), Javascript will convert one to the other format until it finds a match or has failed to do so.
  • x === y is strict equality. (is x equal to y) WITHOUT changing primitive type.

In your example you're assigning values, not testing equality. That's the source of your problems.

Also, your "if" statement is not truly working, since you're also making an assignation, not an equality test.

Post a Comment for "Javascript: Else If Condition Not Working"