Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript True or False Statements …

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #158640
    nathan
    Participant
    if(0) {
    console.log("Welcome");
    } else {
    console.log("Don't let the door hit ya where the good Lord split ya!");
    }
    

    By default since an if statement should be true with the following else statement being false? especially when the two statements are like the above. Right?

    #158641
    TheDoc
    Member

    What’s the question?

    #158644
    nathan
    Participant

    when an if / else is set up … the statement immediately after … in this case … Welcome … is always True … and the preceding statement … Dont let the door hit ya … is always False. Right?

    #158658
    TheDoc
    Member

    Yes. If the first condition is met the else will never run. But I’m still not sure I understand your question. I feel like you’re trying to ask something more complicated but can’t quite get the words right.

    #158664
    nathan
    Participant

    I took a test with similar question … and the latter statement was true and the former … was false … and while I got the question right … I just thought it was odd …

    I did some reading through my college books and I felt like I was right and the test was wrong …

    Perhaps you could explain a scenario where a developer would want the … if … statement to be false and the … else … to be true … SMH … im rambling so I will stop now …

    #158668
    __
    Participant

    Perhaps you could explain a scenario where a developer would want the … if … statement to be false and the … else … to be true …

    It’s not an issue of “wanting” the if to be true or false, it’s an issue of knowing if it is true or false.

    if( userSaidTurnLeft ){
        turnLeft();
    }
    else{
        turnRight();
    }
    

    I don’t care which way they turn, I just need to know what their choice was.

    when an if / else is set up … the statement immediately after … in this case … Welcome … is always True … and the preceding statement … Dont let the door hit ya … is always False. Right?

    I’m not sure I follow your question.

    As @TheDoc answered, If the if is true, then the if block will be executed. If it’s false, the else block will execute.

    However, in your original example, the if condition is not true. Watch out for that door.

    (Also, just to make sure we are not misunderstanding, the else is not the “preceding” statement; it is the following statement.)

    #158746
    nkrisc
    Participant

    Your example will run the else block because as I understand it, JavaScript considers 0 to be false

    #158763
    nathan
    Participant

    @TheDoc … perhaps I could record my screen and post it on youtube … so you could hear me say what Im thinking … cause its kinda tough to type out exactly what it means …

    @Traq … your right … its not the preceding …

    @nkrisc … your right also …

    Im just a little tangled up right now … thanks for the input …

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.