javascript return undefined

To return ‘undefined’ from a javascript function, use the following code:

var foo = function(){
    return; // this returns 'undefined'
}

When checking for undefined, use this code:

if(typeof(foo()) == 'undefined'){
  alert("bar");
}

This is convenient when creating a getter function that can return a mixed variety of types: string, number, true/false (boolean) or null. This is good – but, what to return when the actual “getting” fails?

When making such a generic getter, checking for undefined can be handy if something went wrong during retrieval of the value.