View Full Version : boolean returns false for strings
kentyler
04-16-2003, 07:45 AM
I have question about Boolean.
debug.write( "the string 'false' is " + Boolean("false") );
returns false, my book says "All other primitive values, except false (but including the string "false"), and all objects and arrays are converted to true.
this seems to work for non zero numbers and arrays, but not for strings
see http://www.seedwiki.com/page.cfm?doc=Boolean&wikiid=1944&editfile=yes&filename=boolean
thanks for bearing with me through this
antun
04-16-2003, 09:35 AM
Hey Kentyler
That's a bug in the Flash runtime. Nonempty strings are supposed to evaluate to true according to ECMA-262, however they only do so if they are comprised of base-10 numbers, whitespace, exponents, plus or minus signs and exponents, because they get converted to a number first.
Here's a quick test I did:
<canvas debug="true">
<script>
<![CDATA[
// Boolean conversion
var a = undefined;
debug.write( 'Undefined: ' + Boolean( a ) );
a = null;
debug.write( 'Null: ' + Boolean( a ) );
a = NaN;
debug.write( 'NaN: ' + Boolean( a ) );
a = 0;
debug.write( '0: ' + Boolean( a ) );
a = 5;
debug.write( 'Pos num: ' + Boolean( a ) );
a = -5;
debug.write( 'Neg num: ' + Boolean( a ) );
a = 'smelly';
debug.write( 'Nonempty string: ' + Boolean( a ) );
a = '2343';
debug.write( 'Nonempty numeric string: ' + Boolean( a ) );
a = '';
debug.write( 'Empty string: ' + Boolean( a ) );
a = new Array();
debug.write( 'Array: ' + Boolean( a ) );
a = new LzView();
debug.write( 'View: ' + Boolean( a ) );
]]>
</script>
</canvas>
The results from the debugger window:
Undefined: false
Null: false
NaN: false
0: false
Pos num: true
Neg num: true
Nonempty string: false
Nonempty numeric string: true
Empty string: false
Array: true
View: true
thanks for bearing with me through this
Don't worry - ask as many questions as you like!
-Antun
kentyler
04-16-2003, 12:30 PM
Great. That's the stuff I want to know. Good explanations and examples like that i'm copying to laszlouser.org, where they can be organized around a list of the functions.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.