RSS

Tag Archives: type

true or false: it’s not a binary choice

When using javascript functions like the Array Filter Method you need to define a test over a value.
Sometimes I see that developers are confused about “what’s true” and this can generate bugs.

Let’s work with an example.
Given a function that makes some test over a parameter:

// be careful: uneval() is a Firefox non-standard feature
function doTheTest(val) {
    console.log("------------- val " + uneval(val) + " is " + typeof (val));

    if (val)  console.log('val is pseudo true');
    else console.log('val is not pseudo true');
    
    if (val == true)  console.log('val is == true' );
    else console.log('val is not == true');
    
    if (val === true)  console.log('val is === true' );
    else console.log('val is not === true');
    
    if (val != true)  console.log('val is != true' );
    else console.log('val is not != true');
    
    if (val !== true)  console.log('val is !== true' );
    else console.log('val is not !== true');

    // ..and null?
    if (val == null)  console.log('val is == null' );
    else console.log('val is not == null');
    if (val === null)  console.log('val is === null' );
    else console.log('val is not === null');
}

let’s call that function with some “stuff”:

doTheTest(42);
doTheTest(0);
doTheTest("one");
doTheTest("");
doTheTest(null);
doTheTest(); // undefined
doTheTest({a:1, b:11});
doTheTest(doTheTest); // =)

This is the Firebug’s console output, with some highlight from me:

------------- val 42 is number
val is pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val 0 is number
val is not pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val "one" is string
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val "" is string
val is not pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val null is object
val is not pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is == null
val is === null
------------- val (void 0) is undefined
val is not pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is == null            <==
val is not === null
------------- val ({a:1, b:11}) is object
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val function doTheTest(val) { ..code removed.. } is function
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null

Did you expect these results?

Advertisements
 
Leave a comment

Posted by on 2013/11/06 in dev

 

Tags: , , , , , ,