16

Possible Duplicate:
How can I convert a string to boolean in JavaScript?

Hi,

How can I cast a String in Bool ?

Example: "False" to bool false

I need this for my JavaScript.

Thank you for help !

Community
  • 1
  • 1
Patrik
  • 1,119
  • 5
  • 18
  • 37

4 Answers4

29

You can do this:

var bool = !!someString;

If you do that, you'll discover that the string constant "False" is in fact boolean true. Why? Because those are the rules in Javascript. Anything that's not undefined, null, the empty string (""), NaN, or numeric zero is considered true.

If you want to impose your own rules for strings (a dubious idea, but it's your software), you could write a function with a lookup table to return values:

function isStringTrue(s) {
  var boolValues = { "false": true, "False": true, "true": true, "True": true };
  return boolValues[s];
}

maybe.

edit — fixed the typo - thanks @Patrick

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • This will indeed give `false` for `"False"` but won't it also give `false` for `"True"`? – El Ronnoco Jan 04 '11 at 14:42
  • @El Ronnoco well I may be low on caffeine this morning, but I think that because the string "True" is *not* in the lookup table, `falses(s)` will return `undefined`, and `!undefined` is `true`. – Pointy Jan 04 '11 at 14:43
  • Typo: Should be `return !falses[s];` – Patrick McElhaney Jan 04 '11 at 14:44
  • @Pointy Apologies, I was totally skim reading your answer :) – El Ronnoco Jan 04 '11 at 14:48
  • 2
    Why bother with invoking a method? You could just do `var bools={"false":false,"False":false,"true":true,"True":true};` then `var result=bools["False"];` – user113716 Jan 04 '11 at 15:17
  • 1
    @patrick dw yes you're right, you could code it that way - personally the appeal of making everything a function is that it leaves you with some more flexibility later on if you want to revise the implementation. That's just a programming habit that I'm sure has its own downsides :-) – Pointy Jan 04 '11 at 15:24
  • I'd be more in favor of your function implementation if it threw an error when an invalid value is passed. Currently, it will return `true` in that case. That would (in my opinion) do a lot to make the overhead of the invocation worthwhile. – user113716 Jan 04 '11 at 15:32
  • @patrick dw I agree it would be better for it to be smarter, but honestly, zooming out for a moment, I would prefer not to do anything like this at all if I could help it. It's going to be very fragile no matter what because Javascript will already be happy to perform its own boolean interpretations when the function call is accidentally omitted. – Pointy Jan 04 '11 at 16:17
  • this can be simplified to function isStringTrue(s){ return (s.toLowerCase() === 'true' || s.toLowerCase() === 'false'); } – Nicholas Mberev Mar 04 '23 at 11:03
25

You can use something like this to provide your own custom "is true" test for strings, while leaving the way other types compare unaffected:

function isTrue(input) {
    if (typeof input == 'string') {
        return input.toLowerCase() == 'true';
    }

    return !!input;
}
Jon
  • 428,835
  • 81
  • 738
  • 806
9
function castStrToBool(str){
    if (str.toLowerCase()=='false'){
       return false;
    } else if (str.toLowerCase()=='true'){
       return true;
    } else {
       return undefined;
    }
}

...but I think Jon's answer is better!

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
  • With a function named `castStrToBool`, you probably always want it to return a boolean. Therefore, `return String.prototype.toLowerCase.apply(str) == 'true';` would be all that was needed in the function body. – Andy E Jan 04 '11 at 14:49
  • Some telepathic connection going on here, heh? :) – Šime Vidas Jan 04 '11 at 14:49
  • 1
    @Andy E - I figured that the consumer of this function could probably be helped by being informed of unexpected input eg by returning `undefined`. Obviously if you're happy for `29/10/1976` or `1.056` to be parsed as `true` then your suggestion is fine however this is usually going to be indicative of a problem that should probably be getting dealt with before reaching the `catStrToBool` stage. – El Ronnoco Jan 04 '11 at 23:23
  • Correction to my previous comment - I mean *if you're happy for ... to be parsed as* `false` – El Ronnoco Jan 05 '11 at 11:29
3
function castBool(str) {
    if (str.toLowerCase() === 'true') {
        return true;
    } else if (str.toLowerCase() === 'false') {
        return false;
    }
    return ERROR;
}

ERROR is whatever you want it to be.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385