position and valid should be defined outside the if statement. Otherwise they can be both undefined when the function's return statement is executed.