Search code examples
javascriptif-statementbooleanfunc

JS simple boolean statement in if function - always getting TRUE value


I am learning JS basics and I'm stuck on this super simple boolean inside if statement.

I just keep getting TRUE value from the if statement even when i change the value of the boolean to false: [

    var status = false;
    console.log(status);

    if (status) {
        console.log('true path taken');
    } else {
        console.log('false path taken');
    };

Please help:)


Solution

  • In global scope, status refers to the built-in global variable window.status. Every value assigned to it will be converted to a string:

    status = false;
    console.log(status, typeof status);

    Rename the variable or put your code inside a function:

    (function() {
    
        var status = false;
        console.log(status);
    
        if (status) {
            console.log('true path taken');
        } else {
            console.log('false path taken');
        };
    }());