Additionally to what Kleo wrote, I recommend using the 'use strict'; strict mode, if ES6 is not available, which helps improve code quality and reduce undefined bahvior. One of the things it forbids is using undeclared variables.
Strict mode changes both syntax and runtime behavior. Changes generally fall into these categories: changes converting mistakes into errors (as syntax errors or at runtime), changes simplifying how the particular variable for a given use of a name is computed, changes simplifying
evalandarguments, changes making it easier to write "secure" JavaScript, and changes anticipating future ECMAScript evolution.
(from MDN)