[Solved] How can I differentiate between a boolean and a string return value using JavaScript? [closed]


According to this:

@Esailija Why no sense? If it is true return a true, if it is false
return false, if it is ‘somestring’ also return false. Get it? –
Registered User 31 secs ago

You want

function parseBoolean(value) {
    return typeof value == "boolean" ? value : false;
}

But this obviously won’t pass the test of doom.


This code passes all your tests:

function parseBoolean(bool) {
    return typeof bool == "boolean" ? bool : 0;
}

if( parseBoolean('anystring') == false ) {
    alert("");
}

if( parseBoolean('anystring') !== false ) {
    alert("");
}

if( parseBoolean(true) ) {
    alert('');
}

if( !parseBoolean(false) ) {
    alert('');
}

10

solved How can I differentiate between a boolean and a string return value using JavaScript? [closed]