Take the 2-minute tour ×

When building a jQuery plugin that is supposed to be passed a Boolean value, what's the most fool-proof way of converting the input to a Boolean in a user-friendly way?

To be more precise: I am afraid of the case that people might pass a String of 'false' (instead of a plain false) and therefore a simple conversion of !!option or Boolean(option) will return the "wrong" value (!!'false' is true).

At the moment I am checking my var option like this:

if (typeof(option) != 'boolean'){
    if (option === 'false'){
        option = false; //fake false
    } else {
        option = !!option; //everything else is converted as truthy / falsy in a standard manner  

but I was wondering if there is a more elegant and concise way to do so, or is this just the way that JavaScript handles this?

share|improve this question
"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools." -Douglas Adams –  j08691 Apr 29 '12 at 15:53
@j08691 good thing is that I'm a fool myself so prepare for a lot of ingenuity :P Other than that, replace "fool-proof" by "user-friendly" –  m90 Apr 29 '12 at 15:55
Don't try to "fix" such things. Designing an API to coddle incompetent programmers is generally a terrible idea. –  Pointy Apr 29 '12 at 15:59
add comment

3 Answers

up vote 4 down vote accepted

The most fool proof way, if you are afraid of such input, is to do just

if (typeof(option) != 'boolean')
    console.error('Function X expects a Boolean.');

Look at the solution you have now, you didn't take into account 0 or 1. Or what about no or yes?

Strings and Integers aren't meant to be used as Booleans; so, there is no need to parse them.

Take a look at high quality / usage JS frameworks, they don't do this. Why should you?

share|improve this answer
Well, in case the docs state "pass a Boolean" and someone will pass his dogs' name I could live with things going wrong, so I was actually just looking for a way to circumnavigate 'false' problems - other than that you are totally right. –  m90 Apr 29 '12 at 15:58
@m90: How did you determine that to be a problem? –  Tom Wijsman Apr 29 '12 at 16:00
@TomWijsman false is not considered falsy, while true is truthy and it's a common mistake to enclose params with quotes. –  Christoph Apr 29 '12 at 16:03
@m90: People that use your plugin need to adhere to the contract you specify in your documentation; if they don't then it's their fault, not yours. Asserts (or errors) can help you to catch such errors, but you shouldn't go further than that to introduce behavior that isn't specified in your documentation. You could of course go and enumerate what you understand as a Boolean in your documentation, but if you think that way for everything then you're going to have to implement a lot more code and document a lot more. Don't over-think your library, or it can become bad in other ways... –  Tom Wijsman Apr 29 '12 at 16:19
@TomWijsman "you shouldn't go further than that to introduce behavior that isn't specified in your documentation" : 100% convinced, case closed! Thanks for the input –  m90 Apr 29 '12 at 16:24
show 3 more comments

I would do this.

var result = ( userInput === true );

The only way that this result will return true is if the type if a boolean and the value is true. Otherwise everything else is false. Don't waste your time trying to fix other's mistakes.

But.... if you have a lot of time on your hands, then try this.

var getBooleanValue = function( userInput ){
    if( !userInput ){
        return false;
    var boolNames = {
        'true':1, 'yes':1,
    return (userInput in boolNames && !!boolNames[ userInput ])|| ( userInput === true );
var tests = [
    [ true, true ],
    [ 'true', true ],
    [ 'yes', true ],

    [ false, false ],
    [ 'false', false ],
    [ 'no', false ]
var runTest = function( tests ){
    var i = tests.length;
    while( i-- ){
        if( getBooleanValue(tests[i][0]) !== tests[i][1] ){
            throw new Error( "Test error: getBooleanValues( " + tests[i][0] + ") should return " + tests[i][1] );
runTest( tests );
share|improve this answer
+1 for proper testing. -1 for a post-decrement which creates a temporary value on each iteration. +1 for one-liner. –  Tom Wijsman Apr 29 '12 at 16:24
Fixed and updated. –  Larry Battle Apr 29 '12 at 16:30
var boolNames = { 'true':1, 'yes':1, 'false':0,'no':0, 'yourMoM':this.yes }; is invalid. You cannot use this in an object literal. Otherwise, nice solution. –  Thomas Jones Apr 29 '12 at 16:59
Fixed and updated. –  Larry Battle Apr 29 '12 at 17:13
add comment

I would do something like this:

var falsey = ["0", "", "false", "null", "undefined", "NaN"];
var isFalse = false;

for(var i = 0, len = falsey.length; i < len; i++){
    if(options + "" == falsey[i]){
        isFalse = true;

What I did is to stringize both the options and the falsey values 0, "", false, null, undefined, NaN

This will detected them regardless of what they were (technique similar to uppercase both the search word and the comparing word)

Because the list is short, you could also do it with switch cases or else ifs

share|improve this answer
Feeding programmers that make mistakes is a very bad idea. A O(n) for loop and array lookup, really? –  Tom Wijsman Apr 29 '12 at 16:06
@TomWijsman true, maybe switch cases or else ifs would be faster –  ajax333221 Apr 29 '12 at 16:21
@ajax333221 or.. you could just use a hash table which has an average of O(1 + n/k) –  Larry Battle Apr 29 '12 at 16:34
@LarryBattle thanks for your comment/answer, +1 for both. I really learned something valuable :) –  ajax333221 Apr 30 '12 at 20:06
@LarryBattle hi again, I need your special powers to test the performance, because I am doing something wrong jsperf.com/hashtablevsarraylookup –  ajax333221 Apr 30 '12 at 20:28
show 3 more comments

Your Answer


