Invert the `if` statement at the beginning of this function and have an early return for undefined.  It will make the code cleaner.