There is nothing wrong with declaring a variable inside an if-statement block. However, if you're writing ES6 you should consider dropping var from your vocabulary. Use let and const instead (these do follow more logical scoping rules -- whereas var in your case would be scoped to the containing function)