Wednesday 13 June 2012

Javascript Return Statement

After 20+ years of programming I learnt something new today, while messing about with the php_spidermonkey extension.

If you place a carriage return after a 'return' statement in Javascript, and put some code on the line underneath, the function returns 'undefined', as if the statement was simply: return;

Here's an example of a function that returns 'undefined':
function whatelse()
    var ok = false;
    return // function ends here
        ok ? 'OK (true)' : 'OK (false)'; // this line is never executed!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.