Fix references to undefined

See this article for explanation: <http://davidbcalhoun.com/2011/checking-for-undefined-null-and-empty-variables-in-javascript/>