Why " if (youHit) " Not " if (youHit === 1)


#1

Hello there,

Why can you write

while(slaying) {
if (youHit) { blah blah blah

Instead of

while(slaying) {
if (youHit ===1 ) { blah blah blah

In the first example couldn't youHit be 0 in which case you would be prompting the wrong message aka "you hit the dragon" even though you didn't?


#2

    var youHit = 0;
    
    if (youHIt) {
        //
    } else {
        console.log("this will print when youHit is zero");
    }
    
    youHit = 1;
    
    if (youHit) {
        console.log("this will print when youHit is non-zero");
    } else {
        //
    }

Explanation:

In a conditional statement, such as if or while, the condition may be any expression, number, string, boolean, object, etc.. The yield will always be Boolean true or false.

Non-zero numbers, non-empty strings, arrays and objects all evaluate to true.

Zero (0), the empty string ("" or ''), null and undefined all evaluate to false.