If you want to catch yourself from introducing this bug into your code you should use let and const instead, as these are not hoisted. You should also be using a linter that will catch these issues.
@whotheheck If it helps your team. In general I'd say yes to var statements. Or at least make it a rule to declare them before their usage. On the other hand, let and const are block scoped and are meant to be used ad-hoc throughout your code.