Actionscript 2 Mystery of the day

Actionscript

I was just wondering if anyone could give me a good reason why

trace( (0 || 23) );

would trace ‘23′ instead of ‘true’ in AS2.

Bonus points for answers that contain mythical code-gnomes.

This entry was posted on Tuesday, August 22nd, 2006 at 4:14 pm and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

8 Responses to “Actionscript 2 Mystery of the day”

  1. Keith Peters Says:

    the way flash handles boolean ORs is it evaluates the first half. If it evals to true, it returns the first half. If it evals to false, it returns the second half. It doesn’t convert what it returns, just returns the raw value. If you actually had boolean values in there, trace(false || true) it would return the second value, true. But since 0 evaluates to false, it just returns 23.

  2. Sven Says:

    Weird,

    anyway, it works this way:

    var result:Boolean = Boolean(0 || 1);
    trace(result);

  3. Josh Says:

    That’s a good try Keith, but you forgot the part about the code-gnomes toiling for hours in the caves of Ecma to bring us these return values. Reportedly, many gnomes perish each day just to maintain the supply of Boolean return values we developers consume in just one debug session. ;)

  4. Scott Janousek Says:

    You have to enter 4 8 15 16 first, THEN 23, followed by 42!

    Confused?! Watch the orientation film produced by Hanso Corporation.

    :)

  5. adam Says:

    Weird,

    trace((Number(0) || Number(23))); // returns 23
    trace((Number(0) == false ? 0 : 23)); // returns 0

    Should Flash fundamentally be doing the same evaluation on both traces?
    The first trace says, if the first part is true, return it, if not, return the second half. The second trace is doing the same thing, except Im spelling it out for flash. Because, Flash evaluates Number(0) as false.

    Oh yea, no code-gnomes were harmed in the execution of this experiment.

  6. Niocola Marini Says:

    trace( (0 || 23) ); //return 23
    trace( (1 || 23) ); //return 1
    trace((Number(1) == true ? 0 : 23)); // returns 0

    mistery solved, but
    a question … what are code-gnomes ?
    Are like ants, or the small mouse that make cofe in the automated machine
    here in italy? Are code-gnomes that bring me the result of a formula ?
    (somtime they are drunken ! )

  7. Lisandro Puzzolo Says:

    Actually, there is a “bug?” in Actionscript implementation.. The following code doesn’t work:

    function a(){return “a”};
    function b(){return “b”};
    trace ( (a||b) () ); // this should trace “a” but traces “undefined”;

    trace ( Function(a||b)()) ; //this DO trace “a”;

  8. Nikola Says:

    :roll::roll::roll::roll::roll: nice i thing there is error

Leave a Reply