Switched undefined checks to typeof
Fix regression with the val function. Fixes #457