Return in switch statements

Posted on
  • Return within switch statements are handled like a break (and do not return anything).
    See the following example, with execution trace on Espruino and on node.js .

    function foo( arg ) {
        switch (arg) {
        case 'bar': console.log( 'is bar' ); return( 'bar' );
        case 'gee': console.log( 'is gee' ); break;
        default: console.log( 'is default' ); return( 'default' );
        };
        return( 'end' );
    }
    
    console.log( '<--', foo( 'bar' ));
    console.log( '<--', foo( 'gee' ));
    console.log( '<--', foo( 'foo' ));
    
    

    on espruino, shows as

    is bar
    <-- end
    is gee
    <-- end
    is default
    <-- end
    

    on node.js, shows as

    is bar
    <-- bar
    is gee
    <-- end
    is default
    <-- default
    
  • Thanks - I'll take a look at this and will see if I can fix it

  • Ok, it's now fixed, so give the builds a few minutes and you'll have a working version available.

    Thanks again for tracking this down to a small example and an 'expected' + 'what I get' output. It makes it miles easier for me to track problems down.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Return in switch statements

Posted by Avatar for vprunet @vprunet

Actions