Created
January 12, 2010 04:52
-
-
Save josher19/274918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Discussion about the "truthfulness" of Objects of the Boolean class. | |
Given some Javascript code of: | |
var bool_examples = [ | |
true == new Boolean(true), | |
true === new Boolean(true), | |
false == new Boolean(false), | |
false === new Boolean(false), | |
! 0, | |
! new Boolean(false), | |
! "", | |
! new Boolean(true), | |
! new Boolean(false).valueOf(), | |
! {}, | |
]; | |
and asked, "how many of these values are not equal to true? How many i where bool_examples[i] != true" | |
A newbie will think the first 6 are surely true, will assume that this one is false: | |
! new Boolean(true), | |
but might not be so sure about these two: | |
! "", | |
! {}, | |
.. especially if they are Java programmers and expect a Compiler Error. | |
Someone with a little experience who has used the || operator will think that the only false values are: | |
! new Boolean(true), | |
! {}, | |
.. as they say, a little experience is a dangerous thing. | |
Someone who has used the === operator a lot will know that it checks type as well as value, so these are also (surprisingly) false: | |
true === new Boolean(true), | |
false === new Boolean(false), | |
A programming logician or someone who has tried to use the Boolean class will recognize that | |
new Boolean(value) always returns an Object, so | |
! new Boolean(false), | |
is much like | |
! new Object(initialValue) | |
which is therefore the same as | |
! {} | |
so therefore | |
! new Boolean(false), | |
is false ("What!?" you say? Yes, try it: !new Boolean(false ), and will avoid using the Boolean class at all costs and use boolean primitives (true, false) instead, so _always_ downcast to a primitive using b.valueOf(). So the false values are: | |
true === new Boolean(true), | |
false === new Boolean(false), | |
! new Boolean(false), | |
! new Boolean(true), | |
! {}, | |
Finally, a JavaScript Ninja will notice the extra Comma at the end of the Array: | |
! {}, | |
]; | |
and know that the compiler inserts an "undefined" value there, | |
! {}, | |
undefined | |
]; | |
and that undefined != true, so the number of values which are not true in bool_examples is 6! | |
Moral of the story: Avoid the Boolean class and use primitives (true,false) instead. | |
If a Javascript Library you use is unfortunate enough to use Booleans, get a new Library or downcast you Booleans to primitives using the .valueOf() method call as soon as possible. | |
Bonus lesson: dangling commas will not throw an Exception (as Java does) but will insert an "undefined" value and increase the length of your array by 1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment