Skip to content

TypeError: if ‘false’ not working as expected

I’m doing a PWA quiz application using React.js and I’ve met the following problematic:

I can get questions objects with only one answer, and some with multiple.

In the case there is only one possible answer, I want to force the user to only have one possibility.

To do that, I made the following algorithm:

  clickOnChoice = (key) => {
    if (this.state && this.state.correctAnswers) {
      let newChoices = INITIAL_CHOICES; // {}
      if (this.state.multiChoice) {
        console.log("this.state.multiChoice:", this.state.multiChoice); // this.state.multiChoice: false ???
        newChoices = JSON.parse(JSON.stringify(this.state.choices)); // {answer_b: 1}
      newChoices[key] = 1 - (newChoices[key] | 0); // {answer_b: 1, answer_a: 1}
        choices: newChoices
      }, this.updateNextButtonState);

However the execution seems to ignore the condition if (this.state.multiChoice).

What am I missing?


Maybe I need a cup of coffee… ☕

Anyway, thanks in advance!


It is more than likely you are trying to checking a string of ‘false’ rather than an actual boolean value.

you can check that the string is the expected boolean if (this.state.multiChoice === 'true') or change the value of the state property to true || false