If I have a javascript function, it is possible that I do not return a value from every code path, e.g:
function f()
{
if (checkSomething())
{
return 42;
}
// no return statement here
}
Is this valid javascript or does it only work by chance (i.e. might there be problems when run in some browsers)?
Yes, it is valid javascript. If
checkSomething()evaluates tofalse, it will returnundefined.See http://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope for more details.
Althought, you should note that returning different types of data (In this case, an integer or
undefined) is bad practice.